17

What exactly I need to do to make python's unittest work? I checked the official documentation, SO questions and even tried using nose, but nothing worked so far. What I'm doing wrong?

bash:~/path/to/project/src/tests$ ls -l
total 8
-rw-r--r-- 1 myuser myuser 342 Out 11 11:51 echo_test.py
-rw-r--r-- 1 myuser myuser  71 Out 11 11:28 __init__.py
bash:~/path/to/project/src/tests$ python -m unittest -v echo_test

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
bash:~/path/to/project/src/tests$ python -m unittest discover

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK
bash:~/path/to/project/src/tests$ cat echo_test.py
import unittest

class EchoTest(unittest.TestCase):  
    def fooTest(self):
        self.assertTrue(1==1)

    def barTest(self):
        self.assertTrue(1==2)

#suite = unittest.TestLoader().loadTestsFromTestCase(TestEcho)
#unittest.TextTestRunner(verbosity=2).run(suite)

if __name__ == '__main__':
    unittest.main()

As you can see, the tests simply aren't run and I have no idea why(since I'm not a python programmer). Just for information, I'm using python 2.7 and the __init__.py is an empty file. Any thoughts?

Rafael Ibraim
  • 1,515
  • 2
  • 12
  • 13

4 Answers4

31

You need to rename the methods to begin with the word "test".

As seen on http://docs.python.org/library/unittest.html :

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.

18

I would like to mention that you CANNOT run a python unit test as though it were an executable python file. For example, this was my problem:

python -m unittest ./TestMyPythonModule.py

... (stack trace) ...
ValueError: Empty module name

It fails.

However, this works:

python -m unittest TestMyPythonModule

It's easy to overlook at first.

NuclearPeon
  • 5,743
  • 4
  • 44
  • 52
  • 2
    just to add my cents here. if the test module `TestMyPythonModule.py`resides in the folder `tests`, the correct command is `python -m unittest tests.TestMyPythonModule`. Sometimes the integrated terminals in IDEs autocomplete it like `python -m unittest .\tests\TestMyPythonModule`, but this gives an `Empty module name` error. – MichaelHuelsen Oct 07 '19 at 11:36
3

unittest.main() will run all the function that begin by "test". So you should rename your functions

class EchoTest(unittest.TestCase):  
    def testfoo(self):
        self.assertTrue(1==1)

    def testbar(self):
        self.assertTrue(1==2)
  • 2
    This worked for me. One other thing I had to do to get discovery working (instead of calling unittest.main()) was to rename the file from name_test.py to test_name.py – Rusty Rob Apr 29 '13 at 23:18
0

Test functions must start with the name test So fooTest should be testFoo. See the docs here

Also, there isn't a need for the __init__.py file, assuming those are the only two files in your tests directory.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
brc
  • 5,281
  • 2
  • 29
  • 30