70

I'm getting stuck with some unittests.

Here's the simplest example I could come up with:

#testito.py
import unittest
class Prueba(unittest.TestCase):
 def setUp(self):
 pass
 def printsTrue(self):
 self.assertTrue(True)
if __name__=="__main__":
 unittest.main()

Problem is, running this has no effect:

$ python testito.py 
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK

I'm scratching my head as I don't see any problem with the code above. It happened with a couple of tests now and I don't really know what to do next. Any idea?

asked Nov 29, 2012 at 13:07

1 Answer 1

125

By default, only functions whose name that start with test are run:

class Prueba(unittest.TestCase):
 def setUp(self):
 pass
 def testPrintsTrue(self):
 self.assertTrue(True)

From the unittest basic example:

A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests.

answered Nov 29, 2012 at 13:10
Sign up to request clarification or add additional context in comments.

10 Comments

just wandering why testPrints instead of test_prints
@FooBarUser: I was sticking to the existing naming convention of the original question.
Doesn't work for me even adding test at the beginning of the function. It's just the same as this, but 0 tests are run...
@ScottSkiles: you'd have to define your own TestLoader subclass, overriding the getTestCaseNames() method. The current implementation makes this a little simpler; you can also set the testMethodPrefix attribute on an existing loader.
@ScottSkiles: Some helper functions in unittest.loader do just that.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.