pytest 编写测试用例的话,默认的用例规范是:
首先,模块名需要以test_开头;有一次直接用testdemo定义用例模块名,pytest运行时没有发现,说明需要时test_开头
1、用例文件:test_开头
2、以test_开头的函数会当成一条测试用例;
3、以Test开头的类会当成测试用例类;
测试类中,以test开头的方法会当成一条测试用例;
注意点:用例规范可以通过pytest.ini去配置
另外 ,我们学习pytest可以参考pytest的官方,https://docs.pytest.org/en/latest/contents.html。也是一个锻炼你英文的机会哦!
如果自己定义规则的话,pytest.ini配置文件里进行定义,该文件放在和run.py同一目录;
如下面这段测试案例代码
def test_o1(): assert 1 == 1 def l_test(): assert 2 == 3 class TestLogin: def test_02(self): assert 33 == 33
直接运行,pytest会找到两个测试方法
import pytest pytest.main()
测试结果为:
============================= test session starts ============================= platform win32 -- Python 3.6.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 rootdir: E:\pystudy\web01\xxxx, configfile: pytest.ini plugins: allure-pytest-2.9.45, forked-1.3.0, html-3.1.1, metadata-1.11.0, ordering-0.6, rerunfailures-10.2, xdist-2.4.0 collected 2 items test_login.py .. [100%] ============================== 2 passed in 0.04s ============================== Process finished with exit code 0
二、如果需要调整pytest查找测试方法的规则,可以进行下面这样定义:
pytest.ini
[pytest] python_files = test*.py python_functions = test* *test
python_classes =
Test*
*Test
python_functions,可以设置增加pytest发现测试方法的规则;默认是test*;上面配置新增了test*;
python_classes,可以设置测试类的类名规则;类名默认是Test*是测试类,上面新增了*Test;
默认的测试文件名称是test*.py格式的;
这样再次运行run.py,可以得到如下结果,发现了3个测试方法;
collected 3 items test_login.py .F. [100%] ================================== FAILURES =================================== ___________________________________ l_test ____________________________________ def l_test(): > assert 2 == 3 E assert 2 == 3 test_login.py:6: AssertionError =========================== short test summary info =========================== FAILED test_login.py::l_test - assert 2 == 3 ========================= 1 failed, 2 passed in 0.07s =========================
三、pytest的测试数据驱动方法
@pytest.mark.parametrize('item',cases)
标签:定义,py,assert,用例,pytest,Test,test From: https://www.cnblogs.com/yxm-yxwz/p/16484779.html