Pytest进阶 -- fixture自动应用
Fixture 在自动化中的应用 - 自动应用
场景:
不想原测试⽅法有任何改动,或全部都⾃动实现⾃动应⽤,
没特例,也都不需要返回值时可以选择⾃动应⽤
解决:
使⽤ fixture 中参数 autouse=True 实现
步骤:
在⽅法上⾯加 @pytest.fixture(autouse=True)
案例
conftest.py
import pytest
@pytest.fixture(scope="function",autouse=True) #加入后所有case会自动加上这个方法
def login():
print("\nlogin.....\n")
token = 123
yield token #返回token值
print(f"\nlogout.....\n")
test_demo.py
def test_search():
print("search")
def test_order(): #没有加入login,但还是会执行login,因为fixture设置了autouse
print("ordering")
def test_cart():
print("shopping cart..")
class TestDemo:
def test_case_1(self):
print("test case 1")
def test_case_2(self):
print("test case 2")
输出
login.....
PASSED [ 20%]search
logout.....
test_demo.py::test_order
login.....
PASSED [ 40%]ordering
logout.....
test_demo.py::test_cart
login.....
PASSED [ 60%]shopping cart..
logout.....

浙公网安备 33010602011771号