[Answered ]-Skipping a test based on the pass of a previous test

2👍

Here is how I’d do it:

  1. Have a global variable called test_condition_a = False shared among test cases.
  2. Try/catch assert in test_a and catch the exception when it fails so that you can set test_condition_a = True before the test exits.
  3. Use @unittest.skipIf(test_condition_a) on all other test cases that you want run only if test_a fails.

EDIT On second thought, the above is not going to work as the test order is random. Your best bet would be to do something like this

class Test(TestCase):
    def setUp(self):
        ...

    @unittest.skip("Skip by default")
    def testB(self):
        #test code

    def testA(self):
        try:
            #test code
            return True
        except Error:
            return False

    def testA_then_B(self):
        if (self.testA()):
            self.testB()
👤Lim H.

Leave a comment