views:

34

answers:

2

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.

A: 

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.

Mark Roddy
Those look like tools for discovering unit tests. Maybe I should clarify, the files that are going to be put into the folder are data files (binary files and images), both of which can be processed with a standard algorithm. So unless an individual test is written for each file, I don't see how this could be used.
Leif Andersen
Ah, ok. I didn't get that from the original question. So you're trying to deal with the 'for x in y: test(x)' problem where the test fails on a particular 'x', but you want it to run on all in 'y' regardless?
Mark Roddy
Basically yes. I would also like to output that to the user in a format similar to individual tests, but that's trivial to do once I get this up and running. Thank you.
Leif Andersen
A: 

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.

Leif Andersen