网站备案流程和规则,建设政务门户网站的基本意义,做软件的步骤,网站建设优化课程pytest的fixture中文介绍可参考#xff08;不过文档稍微有点老#xff09;#xff1a; https://www.osgeo.cn/pytest/fixture.html#what-fixtures-are
pytest各个作用域的fixture
scope “function” 可作用于每个用例 fixture使用的声明放在类定义前面#xff0c;类中的…pytest的fixture中文介绍可参考不过文档稍微有点老 https://www.osgeo.cn/pytest/fixture.html#what-fixtures-are
pytest各个作用域的fixture
scope “function” 可作用于每个用例 fixture使用的声明放在类定义前面类中的每个用例执行时都会调用fixture装饰函数 fixture使用的声明放在用例前用例执行时会调用fixture装饰函数scope “class” 作用于整个类 fixture使用的声明放在类定义前面类中的第一个用例执行时会调用fixture装饰函数一次 fixture使用的声明放在类中的用例前用例执行时会调用fixture装饰函数一次之后的用例即时有pytest.mark.usefixtures(“fixture_class”)也不会执行scope “module” 作用于整个python文件 在整个python文件中只会调用一次 不管pytest.mark.usefixtures(“fixture_module”)声明放在哪里类外的用例前、类的声明前或这个类中的用例前fixture函数都只会在整个python文件执行第一个用例时调用一次scope “session” 作用于整个会话通常可以放在conftest.py文件中作为全局使用的前、后置步骤 在所有地方都可以使用pytest.mark.usefixtures(“fixture_session”)
例
import pytestpytest.fixture(scopefunction)
def fixture_function():print(fixture_function ####)pytest.fixture(scopeclass)
def fixture_class():print(fixture_class ----)pytest.fixture(scopemodule)
def fixture_module():
print(fixture_module )def test_0():print(test_0)pytest.mark.usefixtures(fixture_class)
pytest.mark.usefixtures(fixture_function)
class Test_class():pytest.mark.usefixtures(fixture_module)def test_1(self):print(test_1)def test_2(self):print(test_2)def test_3(self):print(test_3)def test_4(self):print(test_4)打印内容如下 4 passed in 0.23s
fixture_module
fixture_class ----
fixture_function ####
PASSED [ 25%]test_1
fixture_function ####
PASSED [ 50%]test_2
fixture_function ####
PASSED [ 75%]test_3
fixture_function ####
PASSED [100%]test_4Process finished with exit code 01.2.pytest添加fixture装饰实现前置、后置方法
可以通过fixture夹具实现前置后置方法后置需要使用yeild来实现。 如果一个方法或者一个类想要同时调用多个fixture。有两种方法
可以使用pytest.mark.usefixtures()进行叠加。 注意叠加顺序先执行的后添加pytest.mark.usefixtures语句后执行的先添加。 需注意与直接传入fixture不同的是pytest.mark.usefixtures无法获取到被fixture装饰的函数的返回值 pytest.mark.usefixtures的使用场景是被测试函数需要多个fixture做前后置工作时使用可以在方法中添加多个fixture函数名作为入参执行顺序先入参的后调用。
具体如下
import pytestclass Test_hhh:pytest.fixturedef setup_step1(self):print(setup_step1 )pytest.fixturedef setup_and_teardown_step2(self):print(setup_step2 ####)yieldprint(teardown_setp2 ####\n)pytest.mark.usefixtures(setup_step1)def test_1(self):print(test_1)pytest.mark.usefixtures(setup_step1)pytest.mark.usefixtures(setup_and_teardown_step2)def test_2(self):print(test_2)def test_3(self, setup_step1, setup_and_teardown_step2):print(test_3)def test_4(self, setup_and_teardown_step2):print(test_4)setup_step1()、setup_and_teardown_step2()两个函数前添加了 pytest.fixture 装饰scope默认是function。 每次执行test_1用例之前都会先调用setup_step1()。 每次执行test_2用例之前都会先调用setup_and_teardown_step2()再调用setup_step1()。 每次调用test_3用例之前都会先调用setup_and_teardown_step2()再调用setup_step1()。 每次调用test_4用例之前都会先调用setup_and_teardown_step2()。 打印顺序如下 4 passed in 0.21s
setup_step1
PASSED [ 25%]test_1
setup_step2 ####
setup_step1
PASSED [ 50%]test_2
teardown_setp2 ####setup_step1
setup_step2 ####
PASSED [ 75%]test_3
teardown_setp2 ####setup_step2 ####
PASSED [100%]test_4
teardown_setp2 ####Process finished with exit code 0pytest中各个级别的setup和teardown方法
除了使用fixture来实现前置、后置。pytest也可以直接使用setup和teardown方案实现前置、后置。pytest的前置、后置分为方法级、类级和模块级。
方法级setup_method() teardown_method()类级别setup_class() teardown_class()模块级setup_module() teardown_method()
def setup_module():print(setup_module)def teardown_module():print(teardown_module)def test_1():print(test_1)assert 1 1class Test_learn:def setup_method(self):print(setup_method)def teardown_method(self):print(teardown_method)def setup_class(self):print(setup_class)def teardown_class(self):print(teardown_class)def test_2(self):print(test_2)assert Truedef test_3(self):print(test_3)在整个文件中的第一个用例执行前setup_module()会被调用。 在整个文件中的最后一个用例执行后teardown_module()会被调用。 在类中的第一个用例执行前setup_calss()会被调用。 在类中的最后一个用例执行后teardown_class()会被调用。 在类中的每个用例执行前setup_method()会被调用。 在类中的每个用例执行后teardown_method()会被调用。 打印结果如下 test session starts
collecting ... collected 3 itemstest_file.py::test_1
setup_module
test_1
PASSED
test_file.py::Test_learn::test_2
setup_classsetup_method
test_2
PASSEDteardown_methodtest_file.py::Test_learn::test_3
setup_method
test_3
PASSEDteardown_method
teardown_class
teardown_module 3 passed in 0.04s pytest参数化
可以使用pytest.mark.parametrize进行参数化具体用法如下
import pytestclass Test_learn:test_data [[正常登录, user1, password1, 登录成功],[密码错误, user2, password2, 用户名或密码错误请重新输入]]ids [i[0] for i in test_data]pytest.mark.parametrize(case, username, password, expect_text, test_data, idsids)def test_1(self, case, username, password, expect_text):print(\n,case,username, password, expect_text)assert Truedata2 [{username: 张三, password:123456},{username: 李四, password: 123456}]pytest.mark.parametrize(dic,data2)def test_2(self, dic):print(\n,dic[username], dic[password])assert Truepytest.mark.parametrize()中第一个参数参数名可以是一个或多个第二个参数test_data为具体数据第三个参数ids为用例名参数名和数组中元素按顺序对应取值。 如上第一个用例中数据有两条。第一条参数case‘登录成功’,username‘user1’, password‘password1’, expect_text‘登录成功’第一条用例的名称登录成功’。 参数的数据也可以是字典、元组等。 执行打印如下 test session starts
collecting ... collected 4 itemstest_file.py::Test_learn::test_1[\u6b63\u5e38\u767b\u5f55] 正常登录 user1 password1 登录成功
PASSED
test_file.py::Test_learn::test_1[\u5bc6\u7801\u9519\u8bef] 密码错误 user2 password2 用户名或密码错误请重新输入
PASSED
test_file.py::Test_learn::test_2[dic0] 张三 123456
PASSED
test_file.py::Test_learn::test_2[dic1] 李四 123456
PASSED 4 passed in 0.04s pytest跳过用例
无条件跳过用例 pytest.mark.skip条件成立时跳过用例 pytest.mark.skipif(表达式, reason‘跳过用例的原因’) 如
import pytest# 会被跳过的
pytest.mark.skipif(4%2 0, reason跳过用例的原因)
def test_skip_if_true():var worldassert var world# 不会被跳过的
pytest.mark.skipif(5%2 0, reason跳过用例的原因)
def test_skip_if_false():var 2assert var 2pytest.mark.skip
def test_skip():var HELLOassert var HELLO打印结果 test session starts
collecting ... collected 3 itemstest_file.py::test_skip_if_true SKIPPED (跳过用例的原因)
Skipped: 跳过用例的原因test_file.py::test_skip_if_false PASSED
test_file.py::test_skip SKIPPED (unconditional skip)
Skipped: unconditional skip 1 passed, 2 skipped in 0.03s pytest指定用例的执行顺序
需要安装插件 pip install pytest-ordering 通过如下装饰器来指定执行顺序order为整型值越小越先执行。
pytest.mark.run(ordern)n0的情况有装饰器的用例比没有顺序装饰器的用例先执行 n0的情况有装饰器的用例比没有顺序装饰器的用例后执行 例子
import pytestpytest.mark.run(order1)
def test_1():var worldassert var worldpytest.mark.run(order0)
def test_2():var 2assert var 2pytest.mark.run(order-1)
def test_3():var HELLOassert var HELLOdef test_4():assert True执行结果 test session starts
collecting ... collected 4 itemstest_file.py::test_2 PASSED
test_file.py::test_1 PASSED
test_file.py::test_4 PASSED
test_file.py::test_3 PASSED 4 passed in 0.02s pytest用例执行重试
需要安装第三方插件 pip install pytest-rerunfailures 使用方式有两种 直接在命令行指定 reruns表示最大重试次数reruns-delay表示每次重试前的等待时间 pytest -s -q --reruns2 --reruns-delay5 {cases_path} --alluredir {report_path}使用pytest.mark.flaky装饰器,reruns表示最大重试次数reruns_delay表示每次重试前的等待时间如 pytest.mark.flaky(reruns3, reruns_delay2) 例
import pytestpytest.mark.run(order-1)
def test_1():var worldassert var worldpytest.mark.flaky(reruns3, reruns_delay2)
def test_4():assert False执行结果如下test_4执行了四次因为执行失败进行重试而执行的有三次。 test session starts
collecting ... collected 2 itemstest_file.py::test_4 RERUN
test_file.py::test_4 RERUN
test_file.py::test_4 RERUN
test_file.py::test_4 FAILED
test_file.py:7 (test_4)
pytest.mark.flaky(reruns3, reruns_delay2)def test_4():assert False
E assert Falsetest_file.py:10: AssertionErrortest_file.py::test_1 PASSED 1 failed, 1 passed, 3 rerun in 6.15s 进程已结束退出代码为 1