Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

unit testing - Python unittest and test discovery

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...