py.test main() 调用示例

Par*_*rth 5 python unit-testing pytest

我有这个结构。

 /bin 
   __init__.py
   run_test.py (call pytest.main)
 /tests
   __init__.py
   test_xyz.py
Run Code Online (Sandbox Code Playgroud)

如果我仅通过 pytest.main() 调用 run_test.py,它不会调用“tests”模块中的测试。我尝试传递几个参数,例如 module="tests" 等,但它们不起作用。我必须完全放弃自动发现并使用 suite 参数来获取任何测试。

我缺少什么?我尝试浏览 pytest 模块中的代码,但它太复杂而难以理解。而且文档非常糟糕。

Ped*_*dru 3

您可以传递要执行的内容的完整路径。

import pytest

pytest_args = [
    '/tests',
    # other tests here...
]
pytest.main(pytest_args)
Run Code Online (Sandbox Code Playgroud)

这将执行在目录中找到的所有测试,不需要/tests/__init__.py