【发布时间】:2015-08-10 02:13:06
【问题描述】:
我有一个文件test_gather.py
import gather
class TestGather(unittest.TestCase):
def test_01_gather(self):
self.assertEqual(len(gather.lookup_terms) > 2, True)
if __name__ == '__main__':
unittest.main()
这在 coveralls 中运行,但永远不会到达最后一行。 (https://coveralls.io/builds/3180464/source?filename=tests%2Ftest_gather.py)
下面是我的travis.yaml:
language: python
python:
- "3.4"
before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
install:
- pip install .
- pip install coverage
- pip install nose coverage
- pip install coveralls
script:
- python setup.py nosetests --with-coverage --cover-package pypiview
- coverage run --source=rawdata setup.py test
- nosetests --with-coverage
after_success:
coveralls
如何配置它以确保所有tests/test_*.py 文件都运行,以便最后一行也被执行?
【问题讨论】:
-
最后一行没有被执行的可能原因是它作为模块的一部分被导入,并且它本身没有被直接执行。那一定是坏事吗?如果您正在运行整个套件,您的鼻子测试是否要求该声明明确存在?
-
是的,如果我不想从命令行手动运行测试,我可以删除该测试,但我认为可能有一种方法可以调用覆盖率来运行它们,就像它们单独运行一样(即覆盖运行 test_gather.py)
-
可能不会。可能是它通过其他方式调用测试,即运行自己的
__main__命名空间,从而将您的测试作为模块导入。所以为什么他们不会被击中是有道理的。我鼓励您查看如何使用您正在使用的工具集从 CLI 运行单个测试,而不是使用__name__部分,因为这不能自动保证执行,如此处所示。跨度> -
我们之前不是讨论过您如何以三种不同的方式运行测试,而您只需要其中一种吗?
-
:) 嗨,内德!是的,我确实尝试过——事实上,很多组合都在使用配置,但发现测试无法运行——决定让它们运行多次
标签: python unit-testing coverage.py