Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally continuing with Python unit tests

I have a test rig with three sets of tests for a sudoku solver:

  1. Test most functions individually
  2. Test the final few functions, which use those in step 1
  3. Test the final few functions with 95 harder puzzles

As step 3 takes a while, I'm looking for a way to only go to the next step if all the previous steps passed. Is it possible to do this using the unit test framework, or is it easier to write my own control structure?

like image 205
Dean Barnes Avatar asked Dec 28 '22 08:12

Dean Barnes


1 Answers

Using doctest

If you wish to use doctest, then treat it just as Python IDLE (see doctest documentation for tips on how to prepare tests), assign results of the previous tests to some variables and then use conditional checks for determining whether you wish to execute other tests. Just like that.

Unfortunately it has nothing to do with unittest.

Using unittest

Command-line options for unittest

When it comes to unittest, you can still invoke it from command line using -f (--failfast) option, so it stops test run on the first error or failure.

Skipping tests

unittest supports skipping tests based on some conditions (see documentation) or when you know the test is broken and you do not want to count it as failure. See basic skipping example:

class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    @unittest.skipIf(mylib.__version__ < (1, 3),
                     "not supported in this library version")
    def test_format(self):
        # Tests that work for only a certain version of the library.
        pass

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass

Was it helpful?

like image 80
Tadeck Avatar answered Dec 31 '22 12:12

Tadeck