关键字:-k
pytest运行参数 -k用于运行指定的测试用例,比如在某一个py文件中,进行脚本调试,此时只想运行某一个用例,则可以使用该参数。举例如下:
import pytest
class TestPytestFeatures(object):
def test_case_login(self):
num = 1
assert num == 1
def test_case_logout(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_shopping(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
if __name__ == "__main__":
pytest.main(["-k shop", "test_case_pytest_features.py"])
"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43
collected 3 items / 2 deselected / 1 selected
test_case_pytest_features.py . [100%]
======================= 1 passed, 2 deselected in 0.36s =======================
"""
此时还可以通过关键字 or 指定多个脚本进行运行,举例如下:
import pytest
class TestPytestFeatures(object):
def test_case_login(self):
num = 1
assert num == 1
def test_case_logout(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_shopping(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_walk(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
if __name__ == "__main__":
pytest.main(["-k shop or out or walk", "test_case_pytest_features.py"])
"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43
collected 4 items / 1 deselected / 3 selected
test_case_pytest_features.py ... [100%]
============================== warnings summary ===============================
test_case_pytest_features.py:32
E:icp_capphelptest_case_pytest_features.py:32: DeprecationWarning: invalid escape sequence i
-- Docs: https://docs.pytest.org/en/stable/warnings.html
================= 3 passed, 1 deselected, 1 warning in 0.09s ==================
"""
-k 与 not 和and配合使用,可以在执行用例前,排除某些用例
import pytest
class TestPytestFeatures(object):
def test_case_login(self):
num = 1
assert num == 1
def test_case_logout(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_shopping(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
def test_case_walk(self):
tmp = ["a", "b"]
assert isinstance(tmp, list)
if __name__ == "__main__":
pytest.main(["-k not shop and not out and not walk", "test_case_pytest_features.py"])
"""
运行结果
============================= test session starts =============================
platform win32 -- Python 3.8.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: E:icp_capphelp
plugins: allure-pytest-2.9.43
collected 4 items / 3 deselected / 1 selected
test_case_pytest_features.py . [100%]
============================== warnings summary ===============================
test_case_pytest_features.py:32
E:icp_capphelptest_case_pytest_features.py:32: DeprecationWarning: invalid escape sequence i
-- Docs: https://docs.pytest.org/en/stable/warnings.html
================= 1 passed, 3 deselected, 1 warning in 0.06s ==================
"""



