我已经为我用如下结构编写的包编写了pytest单元测试:
- test
- \_\_init\_\_.py
- conftest.py
- unit\_tests
- \_\_init\_\_.py
- functionmodule\_test.py
现在,packagedir/mypackage/test/ line test.py建立了两个必需的命令行参数,作为测试的固定设备。当我运行这样的东西:
pytest packagedir/mypackage/ --var1 foo --var2 bar所有测试运行并正确读取命令行参数。但是,当我pip安装这个包(它正确地安装了测试和unit_tests包),然后尝试如下所示:
pytest --pyargs mypackage --var1 foo --var2 bar我遇到了这样的错误:
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --var1 foo --var2 bar如果我跑:
pytest --pyargs mypackage测试运行但失败,因为命令行参数不存在。
有人能向我解释一下这里可能出了什么问题吗?我可以猜测并说这是因为--pyargs参数改变了pytest命令行参数的解释,但这只是猜测。我想知道我是否有办法解决这个问题。
的首要目标是允许用户通过pip安装mypackage,然后能够轻松地测试它,而不需要导航到安装.的站点包位置。
发布于 2017-05-02 20:58:02
我将一个__main__.py添加到mypackage/test目录中,并编写了
import os
import sys
HERE = os.path.dirname(__file__)
if __name__ == "__main__":
import pytest
errcode = pytest.main([HERE] + sys.argv[1:])
sys.exit(errcode)现在,我可以用
python -m mypackage.test --myoption=works在安装软件包时,这是可行的。
您可以在使用travis构建时查看此操作:
https://stackoverflow.com/questions/41270604
复制相似问题