Creating unit tests on the fly

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


On Apr 8, 12:10 pm, Roy Smith <r... at panix.com> 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 tohttp://jjinux.blogspot.com/2005/03/python-create-new-class-on-fly.html
> 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 test_case.run() with a TestResult
object to hold the result 3) accumulate or report the results 4)
repeat forever.

Raymond

twitter: @raymondh




More information about the Python-list mailing list