TA的每日心情 | 郁闷 2022-8-29 14:43 |
---|
签到天数: 1 天 连续签到: 1 天 [LV.1]测试小兵
|
Pytest提供了多样化的方式来运行和调试测试用例,本文介绍一些比较常用的方式。
pytest是如何识别测试用例的呢?它默认使用检查以test_.py 或test.py命名的文件名,在文件内部查找以
test打头的方法或函数,并执行它们。
下面是Pytest的官网的一些测试例子,本文用它来进行不同的Pytest运行。
首先安装好Python和Pytest,新建一个test_pytest_markers.py,在Terminal里面可以尝试以下不同的运行
方式。
#Below are test_pytest_markers.py# content of test_server.pyimportpytest@pytest.mark.webtestdeftes
t_send_http():pass# perform some webtest test for your appdeftest_something_quick():passdeftest_an
other():passclassTestClass:deftest_method(self):pass
1. Pytest Marker 机制
对于Pytest的测试用例,可以在每一个测试用例加一个marker,比如pytest运行的时就只运行带有该mar
ker的测试用例,比如下面的@pytest.mark.webtest。
在我们的实际项目中,我们通常把为每个feature建立相应的marker,用于测试时进行不同条件的挑选。
Pytest还支持一个测试用例有多个marker,这个在后续会把相应的实践加入进来。
测试结果如下,只挑选了测试用例带有webtest marker的运行。
- C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -m "webtest" test_pytest_markers.py============================= test session starts =============================platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.execachedir: .cacherootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:collected 4 items test_pytest_markers.py::test_send_http PASSED============================= 3 tests deselected ================================================= 1 passed, 3 deselected in 0.04 seconds ====================
- 同样的,也可以只挑选了测试用例中不带有webtest marker的运行。
- C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -m"not webtest"test_pytest_markers.py============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected4items test_pytest_markers.py::test_something_quick PASSEDtest_pytest_markers.py::test_another PASSEDtest_pytest_markers.py::TestClass::test_method PASSED=============================1tests deselected ==============================
- 2. 选择运行特定的某个测试用例
- 你可以按照某个测试用例的的模块,类或函数来选择你要运行的case,比如下面的方式就适合一开始在调试单个测试用例的时候。
- C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass::test_method============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected5items test_pytest_markers.py::TestClass::test_method PASSED==========================1passedin0.03seconds ===========================
- 3. 选择运行特定的某个类
- 比如你想单独运行下某个类的所有测试用例,可以按照如下操作。
- C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected4items test_pytest_markers.py::TestClass::test_method PASSED==========================1passedin0.04seconds ===========================
- 4 多种组合
- 比如下面,你可以进行以上两种方式的组合挑选你需要运行的测试用例。
- C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v test_pytest_markers.py::TestClass test_pytest_markers.py::test_send_http============================= test session starts =============================platform win32 -- Python2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0--C:\Python27\python.execachedir:.cacherootdir:C:\Users\yatyang\PycharmProjects\pytest_example,inifile:collected8items test_pytest_markers.py::TestClass::test_method PASSEDtest_pytest_markers.py::test_send_http PASSED==========================2passedin0.06seconds ===========================
- 5 用-k进行关键字匹配来运行测试用例名字子串
- 比如用-k选择测试用例的名字中只带有http关键字的运行。
- C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v -k http test_pytest_markers.py============================= test session starts =============================platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.execachedir: .cacherootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:collected 4 items test_pytest_markers.py::test_send_http PASSED============================= 3 tests deselected ================================================= 1 passed, 3 deselected in 0.12 seconds ====================
复制代码
Reference
|
|