Python 工作服未测试 "if name == __main__"

标签 python unit-testing coverage.py

我有一个文件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()

这在工作服中运行,但永远不会到达最后一行。 (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 文件都运行,以便最后一行也被执行?

最佳答案

您正在使用测试发现来查找并运行测试。您根本不需要 if __name__ 子句。删除它即可。

关于Python 工作服未测试 "if name == __main__",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31910748/

相关文章:

python - 如何根据列表中的值更新/创建 pandas 中的列

Python 未绑定(bind)本地错误

java - 如何使用 Junit 测试工具测试 void 方法?

python - pytest-cov 从远程服务器获取自动化覆盖

python - 父类(super class)中的调用方法没有给出我期望的输出?

python - 在 if 循环中添加到字典

unit-testing - 缓存如何解决?独立使用 jasmine 时出现问题

unit-testing - Clojure 相当于 Python doctest?

python - 通过 pytest 使用多处理时如何测量覆盖率?

coverage.py - 如何配置 tox 以便它在单个环境而不是所有环境上运行 pytest 覆盖?