Search code examples
pythonunit-testingnosesystem-testing

How can I include additional tests if a test passes?


I'm using nose to run some system tests, one of which is to test whether a (config) file exists. If this file exists, I'd like to run some additional tests on it. If not, I'd like to skip a bunch of tests.

What's the best approach to take to make nose include the additional tests if the main test passes?


Solution

  • You could use skipTest from the setUp method in your specific TestCase, like:

    import os
    from unittest import TestCase
    
    class MyTest(TestCase):
        def setUp(self):
            if not os.path.exists('configfile'):
                return self.skipTest('config file not found')
    
        def test01(self):
            # Do something with the file
            with open('configfile') as fd:
                self.assertEqual(fd.readlines().__len__(), 0)
    

    test test01 would not run if config file does not exist.