4

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?

2
  • I can tell you for sure, that this is possible in doctest tests, if you would like to know. Commented Dec 21, 2011 at 14:20
  • Done. A little research revealed some useful features of unittest you may be looking for. You did not mention the module you use for unit testing, so I assumed unittest. Commented Dec 21, 2011 at 15:29

2 Answers 2

8

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?

Sign up to request clarification or add additional context in comments.

Comments

3

If you have your tests structured in unittest test suites, you can execute the long-running test suite only if the first test suite passed without errors using the following piece of code:

suite1 = unittest.TestSuite()
suite2 = unittest.TestSuite()
# fill suites with test cases ...
result1 = unittest.TextTestRunner().run(suite1)
if result1.wasSuccessful():
    unittest.TextTestRunner().run(suite2)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.