Python 单元测试和测试发现
我到底需要做什么才能让 python 的unittest 工作?我检查了官方文档,SO问题,甚至尝试使用nose
,但到目前为止没有任何效果。我做错了什么?
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()
正如你所看到的,测试根本没有运行,我不知道为什么(因为我不是 python 程序员)。 仅供参考,我使用的是 python 2.7,并且 __init__.py 是一个空文件。 有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要重命名以“test”一词开头的方法。
如 http://docs.python.org/library/unittest.html 所示:
You need to rename the methods to begin with the word "test".
As seen on http://docs.python.org/library/unittest.html :
我想提一下,你不能运行 python 单元测试,就像它是一个可执行的 python 文件一样。例如,这是我的问题:
它失败了。
然而,这是有效的:
一开始很容易被忽视。
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:
It fails.
However, this works:
It's easy to overlook at first.
unittest.main() 将运行所有以“test”开头的函数。所以你应该重命名你的函数
unittest.main() will run all the function that begin by "test". So you should rename your functions
测试函数必须以名称
test
开头,因此fooTest
应该是testFoo
。请参阅文档此处此外,不需要
__init__。 py
文件,假设这是tests
目录中仅有的两个文件。Test functions must start with the name
test
SofooTest
should betestFoo
. See the docs hereAlso, there isn't a need for the
__init__.py
file, assuming those are the only two files in yourtests
directory.