平面设计可以做网站,小程序电商平台排名,开源门户网站cms,网络推广怎么做目录 pytest简介
基本测试实例
编写测试文件
执行测试
pytest运行时参数
mark标记
Fixture
pytest插件
Allure测试报告
测试步骤 pytest简介
Pytest是一个非常流行的Python测试框架#xff0c;它支持简单的单元测试和复杂的功能测试#xff0c;具有易于上手、功…目录 pytest简介
基本测试实例
编写测试文件
执行测试
pytest运行时参数
mark标记
Fixture
pytest插件
Allure测试报告
测试步骤 pytest简介
Pytest是一个非常流行的Python测试框架它支持简单的单元测试和复杂的功能测试具有易于上手、功能强大、灵活配置等特点已经成为最流行的测试框架之一。
简单易用Pytest的API设计简洁使得编写测试用例非常容易功能强大支持参数化测试、断言、fixture等功能能够满足各种复杂的测试需求。灵活配置可以通过插件扩展功能支持与Selenium、Requests、Appium等工具集成实现Web自动化、接口自动化和App自动化。报告生成结合Allure可以生成美观的测试报告。插件丰富拥有大量的插件如pytest-ordering、pytest-rerunfailures、pytest-xdist等支持用例管理、执行、跳过、失败重跑等功能
安装 pip install pytest 版本检查 pytest --version
基本测试实例
pytest测试文件的命名规则和基本使用方法1测试文件命名测试文件通常以test_开头或以_test 结尾。 2测试函数命名测试函数应以test开头。3运行测试用例在命令行中使用pytest命令可以自动发现并运行所有的测试用例。也可以指定要运行的测试文件或测试函数
编写测试文件
sub_test.py 与test_example.py
#sub_test.py
def sub_num(a,b):return a-b
def test_sub():assert sub_num(2,3)5
#test_example.py
def add_num(a,b):return ab
def test_add():assert add_num(2,3)5
执行测试
命令行执行 pytest 会过滤所有以test开头或结尾的文件然后执行文件匹配所有以test开头的类然后匹配所有以test开头的测试函数并执行
或者pytest ./文件目录 去解析特定文件 pytest运行时参数 -k根据表达式选择测试用例。 -m只运行带有标记的测试用例 -s当测试失败时打印出错误信息。 --ff先运行之前失败的测试用例 -x在第一个测试失败时退出测试运行。 --lf运行上次失败的测试用例 --collect-only仅收集测试用例不运行它们。 --maxfail指定在失败指定数量的测试用例后停止测试 -v输出更详细的测试信息包括测试用例名称及其所在的类。 --tbstyle控制台中的错误信息输出格式例如--tbline仅显示错误的那一行
mark标记
import pytest
def sub_num(a,b):return a-b
def test_sub1():assert sub_num(2,3)-1
pytest.mark.skip(pytest skip)
def test_sub2():assert sub_num(2,2)0
pytest.mark.skipif(condition(11),reasonTrue pytest skipif)
def test_sub3():assert sub_num(2,1)1
pytest.mark.skipif(12)
def test_sub4():assert sub_num(3,1)2
pytest.mark.xfail #预期到的结果
def test_sub5():assert sub_num(3,1)23
pytest.mark.xfail
def test_sub6():assert sub_num(3,1)2
ls[(1,2,3),(1,2,3),(2,4,6)]
pytest.mark.parametrize(n1,n2,sum,ls)
def test_sub7(n1,n2,sum):assert n1n2 sum
#pytest -v -m smoke
#sub_test.py::test_example1 PASSED [ 50%]
#sub_test.py::test_example2 PASSED
pytest.mark.smoke
def test_example1():assert True
pytest.mark.smoke
def test_example2():assert 1 1 2
Fixture
pytest的fixture是一个非常强大的功能主要用于在测试用例执行前后进行初始化和清理操作(类似setup、teardown)。通过使用pytest.fixture()装饰器可以将一个函数声明为fixture。该fixture的名字默认为函数名也可以通过name参数指定别名。测试用例可以通过在参数列表中包含fixture的名字来使用它pytest会在测试函数运行之前执行该fixture。
参数
scope 定义夹具的作用域决定夹具的生命周期。可选值包括 function每个测试函数调用时都会创建新的夹具默认值。 class每个测试类调用时创建一次夹具。 module每个模块调用时创建一次夹具。 session整个测试会话中只创建一次夹具。 autouse 默认为 False。如果设置为 True则夹具会自动应用于所有测试而不需要在测试中显式声明 params 允许为夹具提供多个参数值测试函数将会被多次调用每次使用不同的参数。 name为夹具指定一个名称默认情况下使用函数名
import pytest
pytest.fixture()
def fix_prepare():print(this is fixture prepare)
def test_fixture1(fix_prepare):print(test_fix1)
def test_fixture2(fix_prepare):print(test_fix2)
pytest.fixture(scopemodule)
#db_connection 夹具在整个模块中只会创建一次两个测试函数都共享这个连接。
def db_connection():connection Database Connectionyield connection # 提供夹具的值print(Closing the database connection) # 清理工作# 使用夹具的测试函数
def test_db1(db_connection):assert db_connection Database Connectiondef test_db2(db_connection):assert db_connection Database Connection
pytest.fixture(autouseTrue) # 定义一个自动使用的夹具
def setup_environment(): #会在每个测试函数运行前自动调用无需在测试函数中显式声明print(Setting up the environment)yield #测试固件需要yeild支持teardownprint(Tearing down the environment)
# 测试函数
def test_example1():assert True
def test_example2():assert True
# 定义一个参数化的夹具
pytest.fixture(params[apple, banana, cherry])
def fruit(request):return request.param
def test_fruit(fruit): # 使用夹具的测试函数#fruit 夹具会为每个测试函数提供不同的参数值测试函数将会被调用三次# 分别使用 apple、banana 和 cherry。assert fruit in [apple, banana, cherry]
if __name____main__:pytest.main([-vs,test_fixture.py])
pytest插件
pytest-ordering 这个插件允许你控制测试函数的运行顺序通过使用pytest.mark.run(order1)这样的装饰器 pytest-assume 这个插件引入了一个新的pytest.assume()函数它允许你在一个测试中进行多次断言并在第一次失败后停止测试。 pytest-xdist 这个插件增加了对并行测试的支持可以通过不同的CPU或者机器并行运行测试。 pytest-instafail 这个插件在测试失败时提供实时的失败信息而不是等到测试结束。 pytest-rerunfailures 这个插件可以在测试失败后重新运行它们。 pytest-html 这个插件生成美观的HTML测试报告。 pytest-metadata 这个插件允许你在测试结束后添加元数据。 pytest-cov 这个插件提供了代码覆盖率的报告。 pytest-forked 这个插件是pytest-xdist的一部分但也可以单独使用提供了一种在多个进程中运行测试的方法 pytest-sugar这个插件提供了一种更好的控制台输出可以显示测试进度。
Allure测试报告
测试步骤
1allure的官网下载地址https://github.com/allure-framework/allure2/releases
2解压、配置环境变量cmd 输入allure 检验安装配置
3pip install allure-pytest 执行命令运行测试case pytest --alluredir ./result/ 生成许多json文件等
4allure generate .\result\ -o ./report/ --clean
5windows下 allure generate ./path/to/your/test_results -o ./path/to/report --clean -O可以写成--report--dir或--output 生成测试报告路径 generate生成测试报告--clear清楚报告生成一个新的
6查看测试报告 windows下可以 allure open -h localhost -p 8083 ./report/ 页面加载index.html一致loading当然还可以使用别的方法