I have a test rig with three sets of tests for a sudoku solver:
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?
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
.
unittest
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With