unittest ValueError

Steve Holden steve at holdenweb.com
Mon Jul 24 16:24:14 EDT 2006


Chris Fonnesbeck wrote:
> I have built the following unit test, observing the examples laid out
> in the python docs:
> 
> class testMCMC(unittest.TestCase):
> 
>     def setUp(self):
> 
>         # Create an instance of the sampler
>         self.sampler = DisasterSampler()
> 
>     def testCoalMiningDisasters(self):
>         """Run coal mining disasters example sampler"""
> 
>         print 'Running coal mining disasters test case ...'
> 
>         # Specify the nimber of iterations to execute
>         iterations = 10000
>         thin = 2
>         burn = 5000
>         chains = 2
> 
>         # Run MCMC simulation
>         for i in range(chains):
> 
>             self.failUnless(self.sampler.sample(iterations, burn=burn,
> thin=thin, plot=True))
> 
>             # Run convergence diagnostics
>             self.sampler.convergence()
> 
>             # Plot autocorrelation
>             self.sampler.autocorrelation()
> 
>         # Goodness of fit
>         self.failIf(self.sampler.goodness(iterations/10)['overall'] < 0.05)
> 
> 
> def test():
>     # Run unit tests
>     unittest.main()
> 
> However, when I run test() from the python shell, it dies:
> 
> In [2]: test()
> 
> ----------------------------------------------------------------------
> Ran 0 tests in 0.000s
> 
> OK
> ---------------------------------------------------------------------------
> exceptions.SystemExit                                Traceback (most
> recent call last)
> 
> /Users/chris/<ipython console>
> 
> /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/PyMC/MCMC.py
> in test()
>    2986
>    2987 def test():
>    2988     # Run unit tests
> -> 2989     unittest.main()
>         global unittest.main = <class 'unittest.TestProgram'>
>    2990
> 
> /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/unittest.py
> in __init__(self=<unittest.TestProgram object at 0x10e6c10>,
> module='__main__', defaultTest=None, argv=['/usr/local/bin/ipython'],
> testRunner=None, testLoader=<unittest.TestLoader object at 0x5feb30>)
>     757         self.progName = os.path.basename(argv[0])
>     758         self.parseArgs(argv)
> --> 759         self.runTests()
>         self.runTests = <bound method TestProgram.runTests of
> <unittest.TestProgram object at 0x10e6c10>>
>     760
>     761     def usageExit(self, msg=None):
> 
> /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/unittest.py
> in runTests(self=<unittest.TestProgram object at 0x10e6c10>)
>     795             self.testRunner = TextTestRunner(verbosity=self.verbosity)
>     796         result = self.testRunner.run(self.test)
> --> 797         sys.exit(not result.wasSuccessful())
>         global sys.exit = <built-in function exit>
>         result.wasSuccessful = <bound method
> _TextTestResult.wasSuccessful of <unittest._TextTestResult run=0
> errors=0 failures=0>>
>     798
>     799 main = TestProgram
> 
> SystemExit: False
> Type exit or quit to exit IPython (%Exit or %Quit do so unconditionally).
> 
> In [3]: from PyMC import testMCMC
> 
> In [4]: foo = testMCMC()
> ---------------------------------------------------------------------------
> exceptions.ValueError                                Traceback (most
> recent call last)
> 
> /Users/chris/<ipython console>
> 
> /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/unittest.py
> in __init__(self=<PyMC.MCMC.testMCMC testMethod=runTest>,
> methodName='runTest')
>     206             self.__testMethodDoc = testMethod.__doc__
>     207         except AttributeError:
> --> 208             raise ValueError, "no such test method in %s: %s" % \
>         global ValueError = undefined
>         self.__class__ = <class 'PyMC.MCMC.testMCMC'>
>         methodName = 'runTest'
>     209                   (self.__class__, methodName)
>     210
> 
> ValueError: no such test method in <class 'PyMC.MCMC.testMCMC'>: runTest
> 
> 
> I have no idea why this is happening. My TestCase class begins with
> "test", the method begins with "test", yet no tests are seen. Also, I
> do not get the runTest ValueError. I assumed that you get this method
> for free win TestCase. The examples certainly do not have this method,
> so I am not sure what the problem is.
> 
> Thanks for any help,

Well, it would make a lot more sense to set the test up to be run as a 
main program with

if __name__ == "__main__":
     test()

at the end, then run it. When I did so with your code (after adding an 
import of the unittest module) I got

C:\Steve\Projects\Python>test47.py
E
======================================================================
ERROR: Run coal mining disasters example sampler
----------------------------------------------------------------------
Traceback (most recent call last):
   File "C:\Steve\Projects\Python\test47.py", line 9, in setUp
     self.sampler = DisasterSampler()
NameError: global name 'DisasterSampler' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

This just shows me that more code is missing. Hope this helps.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list