使用@pytest.mark.only运行单个测试
pytest-onl的Python项目详细描述
只运行标记为@pytest.mark.only的测试。如果没有标记,则所有测试都照常运行。
从mocha借来的。
安装
pip install pytest-only
用法
在功能上使用它
importpytestdeftest_that_will_not_run():assert0@pytest.mark.onlydeftest_that_will_run():assert1
$ py.test -v test_example.py =============================test session starts============================== platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /tmp/example/bin/python3.6 cachedir: .cache rootdir: /tmp/example, inifile: plugins: only-1.0.0 collected 2 items test_example.py::test_that_will_run PASSED===========================1 passed in 0.00 seconds===========================
或者在课堂上使用它
importpytestclassTestThatWillNotRun:deftest_that_will_not_run(self):assert0@pytest.mark.onlyclassTestThatWillRun:deftest_that_will_run(self):assert1
$ py.test -v test_example.py =============================test session starts============================== platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /tmp/example/bin/python3.6 cachedir: .cache rootdir: /tmp/example, inifile: plugins: only-1.0.0 collected 2 items test_example.py::TestThatWillRun::test_that_will_run PASSED===========================1 passed in 0.00 seconds===========================
或者在模块上使用它
# test_example.pyimportpytestpytestmark=pytest.mark.onlydeftest_that_will_run():assert1
# test_example2.pydeftest_that_will_not_run():assert0
$ py.test -v test_example.py test_example2.py =============================test session starts============================== platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0 -- /home/they4kman/.virtualenvs/tmp-53d5944c7c78d28/bin/python3.6 cachedir: .cache rootdir: /home/they4kman/.virtualenvs/tmp-53d5944c7c78d28, inifile: plugins: only-1.0.0 collected 2 items test_example.py::test_that_will_run PASSED===========================1 passed in 0.00 seconds===========================
禁用单个测试运行
要运行所有测试,无论是否使用@pytest.mark.only,请通过 pytest的--no-only标志:
$ py.test --no-only
如果已经传递了--no-only(可能是通过addopts传入 pytest.ini,使用--only标志重新启用它:
$ py.test --no-only --only
开发
安装测试/开发要求
$ pip install -r dev-requirements.txt
运行测试
$ py.test
在所有当前支持的平台上运行测试
$ tox