Creating unit tests on the fly

Raymond Hettinger python at
Fri Apr 8 23:50:58 CEST 2011

On Apr 8, 12:10 pm, Roy Smith <r... at> wrote:
> I can even create new test cases from these on the fly with something
> like:
>  newClass = type("newClass", (BaseSmokeTest,), {'route': '/my/newly/
> discovered/anchor'})
> (credit to
> for that neat little trick).  The only thing I don't see is how I can
> now get unittest.main(), which is already running, to notice that a
> new test case has been created and add it to the list of test cases to
> run.  Any ideas on how to do that?

The basic unittest.main() runner isn't well suited to this task.  It
flows in a pipeline of discovery -> test_suite -> test_runner.

I think you're going to need a queue of tests, with your own test
runner consuming the queue, and your on-the-fly test creator running
as a producer thread.

Writing your own test runner isn't difficult.  1) wait on the queue
for a new test case. 2) invoke with a TestResult
object to hold the result 3) accumulate or report the results 4)
repeat forever.


twitter: @raymondh

More information about the Python-list mailing list