我有一个 python 文件,其中包含一些代码以及对该代码的简单测试。
我想从该 python 文件调用 pytest,让它仅收集该文件中的测试,然后运行它们。
例如,foo.py:
# ... various code above...
def test_foo():
foo = Foo()
assert foo()
def test_bar():
bar = Bar()
assert bar.baz
if __name__ == '__main__':
import pytest
pytest.main()
Run Code Online (Sandbox Code Playgroud)
我现在想运行python foo.py并让 pytest 运行该文件中的两个测试,并且仅运行这些测试。
但是,当我运行时python foo.py, pytest 会收集来自我运行命令的路径的所有模块的所有测试。
我如何运行python foo.py并让pytest.main()调用的foo.py仅调用测试foo.py而不调用其他?
小智 5
根据pytest 文档,选项和参数可以传递给pytest.main. 要在 中运行测试foo.py,可以这样做:
# ... various code above...
def test_foo():
foo = Foo()
assert foo()
def test_bar():
bar = Bar()
assert bar.baz
if __name__ == '__main__':
import pytest
pytest.main(["foo.py"])
# consider using below
# pytest.main([__file__])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1238 次 |
| 最近记录: |