Python 单元测试和测试发现 [英] Python unittest and test discovery

查看:69
本文介绍了Python 单元测试和测试发现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

究竟我需要做什么才能使 python 的 unittest 工作?我检查了官方文档,SO 问题,甚至尝试使用 nose,但到目前为止没有任何效果.我做错了什么?

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()

如您所见,测试根本没有运行,我不知道为什么(因为我不是 Python 程序员).仅供参考,我使用的是 python 2.7 并且 __init__.py 是一个空文件.有什么想法吗?

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?

推荐答案

您需要将方法重命名为以test"一词开头.

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

如在 http://docs.python.org/library/unittest.html 上所见 :

测试用例是通过子类化 unittest.TestCase 创建的.这三个单独的测试是用名称以字母 test 开头的方法定义的.此命名约定通知测试运行器哪些方法代表测试.

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.

这篇关于Python 单元测试和测试发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆