I’m writing a module that involves parsing html for data and creating an object from it. Basically, I want to create a set of testcases where each case is an html file paired with a golden/expected pickled object file.
As I make changes to the parser, I would like to run this test suite to ensure that each html page is parsed to equal the ‘golden’ file (essentially a regression suite)
I can see how to code this as a single test case, where I would load all file pairs from some directory and then iterate through them. But I believe this would end up being reported as a single test case, pass or fail. But I want a report that says, for example, 45/47 pages parsed successfully.
How do I arrange this?
I’ve done similar things with the
unittestframework by writing a function which creates and returns a test class. This function can then take in whatever parameters you want and customise the test class accordingly. You can also customise the__doc__attribute of the test function(s) to get customised messages when running the tests.I quickly knocked up the following example code to illustrate this. Instead of doing any actual testing, it uses the
randommodule to fail some tests for demonstration purposes. When created, the classes are inserted into the global namespace so that a call tounittest.main()will pick them up. Depending on how you run your tests, you may wish to do something different with the generated classes.A sample run: