Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2966

Re: Creating unit tests on the fly

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Creating unit tests on the fly
Date 2011-04-10 22:45 -0400
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-D2282D.22451610042011@news.panix.com> (permalink)
References <55ab5243-da42-4933-b3e5-b8189698b11e@m13g2000yqb.googlegroups.com> <e2a1efe6-17bb-4d94-8fcc-b812b41f6b75@d28g2000yqf.googlegroups.com>

Show all headers | View raw


In article 
<e2a1efe6-17bb-4d94-8fcc-b812b41f6b75@d28g2000yqf.googlegroups.com>,
 Raymond Hettinger <python@rcn.com> wrote:

> 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.

OK, this is working out pretty nicely.  The main loop is shaping up to 
to be something like:

    def go(self):
        result = unittest.TestResult()
        while not self.queue.empty():
            route, depth = self.queue.get()
            test_case = self.make_test_case(route)
            suite = unittest.defaultTestLoader. \
                    loadTestsFromTestCase(test_case)
            suite.run(result)
   if result.wasSuccessful():
            print "passed"
   else:
            for case, trace in result.failures:
      print case.id()
      d = case.shortDescription()
      if d:
                    print d
      print trace
      print '------------------------------------'

It turns out there's really no reason to put the test runner in its own 
thread.  Doing it all in one thread works fine; make_test_case() passes 
self.queue to the newly created TestCase as part of the class dict, and 
one of the test methods in my BaseSmokeTest pushes newly discovered 
route onto the queue.  Perhaps not the most efficient way to do things, 
but since most of the clock time is spent waiting for the HTTP server to 
serve up a page, it doesn't matter, and this keeps it simple.

Thanks for your help!

PS: After having spent the last 6 years of my life up to my navel in 
C++, it's incredibly liberating to be creating classes on the fly in 
user code :-)

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar


Thread

Creating unit tests on the fly Roy Smith <roy@panix.com> - 2011-04-08 12:10 -0700
  Re: Creating unit tests on the fly Raymond Hettinger <python@rcn.com> - 2011-04-08 14:50 -0700
    Re: Creating unit tests on the fly Ben Finney <ben+python@benfinney.id.au> - 2011-04-09 08:46 +1000
      Re: Creating unit tests on the fly Roy Smith <roy@panix.com> - 2011-04-08 22:02 -0400
        Re: Creating unit tests on the fly Ben Finney <ben+python@benfinney.id.au> - 2011-04-09 12:15 +1000
    Re: Creating unit tests on the fly Roy Smith <roy@panix.com> - 2011-04-10 22:45 -0400

csiph-web