PyUnit with a variable number of tests

What I would like to do is create a folder where people can put the file for testing, and automatically use pyunit to run the test as a separate test. I am currently doing the following:

class TestName(unittest.testcase):
    def setUp(self):
        for file in os.listdir(DIRECTORY):
            # Setup Tests

    def test_comparison(self):
        for file in os.listdir(DIRECTORY):
            # Run the tests

def suite():
    return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestName),
])

if __name__ == "__main__":
    unittest.TextTestRunner(verbosity=2).run(suite())

Obviously, there are many problems associated with this, for example, if a test fails, everything fails, etc. etc. What I would like to do is configure the pin to run the test (or test case) for each file that is placed in the directory. Now, I think what I can do is create the class mentioned above with only two methods, but in order to do this successfully, I would have to add a context parameter, for example:

def setUp(self, filepath):
    # Do stuff

Then I could put the loop in the main body of the code and run the tests as follows:

def suite():
    return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestName, file),
])

if __name__ == "__main__":
    for file in DIRECTORY:
        unittest.TextTestRunner(verbosity=2).run(suite(file))

, , unittest.TestCase(). , / , ? .

+3
3

nose . :

def test_comparison():
    for file in os.listdir(DIRECTORY):
        yield run_cmp_test, file

def run_cmp_test(file):
    # Run the tests

funcargs py.test.

+2

, , . , , - .

class TestImage(unittest.TestCase)
    pass #Do stuff, as well as using the filename variable

def suite():
    return unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(TestImage),
])

if __name__ == "__main__":
    for filename in os.listdir(sys.path[0]):
        unittest.TextTestRunner(verbosity=2).run(suite())

. ( ). , unittest. - , .

+2

You can do this with testscenarios , something like this:

class MyTest(unittest.TestCase):

    scenarios = [os.listdir(sys.path[0])]

    ...
0
source

Source: https://habr.com/ru/post/1752191/


All Articles