Question

What I would like to do, is create a folder, where people can put in a file for testing, and have pyunit automatically expand in order to run the test as a separate test. Currently, what I'm doing is:

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 is many problems with that, such as if any test fails, the whole thing fails, etc. etc. What I would like to do, is set up pyunit so that it will run a test (or a test case), for each file that is placed in the directory. Now, what I'm thinking I could do is create the class listed above, with just the two methods, but in order to do that successfully, I would have to add in a context parameter, eg:

def setUp(self, filepath):
    # Do stuff

Then I could put the loop in the main chunk of code, and run the tests like this:

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

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

But I'm not sure how to do that without nearly rewriting the hole unittest.TestCase() class. Is there any way I could do that, or is there any other way I can geta dynamically expanding set of tests/test cases depending on how many files are in a folder? Thanks.

Was it helpful?

Solution

You could use nose support for test generators. This would look something like this:

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

def run_cmp_test(file):
    # Run the tests

Alternatively you could also funcargs in py.test.

OTHER TIPS

Okay, I think I found an answer to my question. I would say that I was using global namespaces, but for some reason, my usual knowledge of namespaces fell flat on it's face.

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())

This will run a test case for every filename in the directory. (and folder name). Although I have no idea why I can use filename in the unittest class. It must have something to do with the way those macros load and run the class.

You can do this with the testscenarios, module, something like this:

class MyTest(unittest.TestCase):

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

    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top