1.pytest fixture
定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture()
fixture命名与用例区分开,不要以test开头。fixture是有返回值的,没有返回值默认为None。
用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。
主要解决测试前后的问题和依赖问题。
2. pytest.fixture()运行配置
fixture的功能主要是定制化执行特定的方法。
比如淘宝,有些不需要登录也可以进行直接操作,比如搜索,对商品进行搜索,不需要登录。但是加入购物车或者支付,都需要登录,才能进行操作。
核心:@pytest.fixture()
括号里面的默认值是function。
操作步骤:
1、导入pytest
2、在登录函数上,使用@pytest.fixture(),注意:函数名称不以test开头
3、不传入变量函数名,直接执行测试函数
4、传入变量函数名,先登录在执行测试函数
import pytest
@pytest.fixture()
def login():
print('登录用户名和密码')
def test_search():
print('用例1,不需要登录也可以进行搜索')
def test_cart(login):
print('用例2,需要登录才可以进行加入购物车')
def test_pay(login):
print('用例3,需要登录才可以进行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回内容:
test_fixture.py::test_search 用例1,不需要登录也可以进行搜索
PASSED
test_fixture.py::test_cart 登录用户名和密码
用例2,需要登录才可以进行加入购物车
PASSED
test_fixture.py::test_pay 登录用户名和密码
用例3,需要登录才可以进行支付
PASSED
3.pytest.fixture(params=xx)数据传递
如上例子中,登录中的用户名和密码,不在函数中,是一个变量,那就需要参数传递,fixture通过params进行操作。@pytest.fixture(params=xxx),函数中request参数是固定
import pytest
@pytest.fixture(params=[1,2,4,'linda'])
def prepare_data(request): #固定写法,传入参数:request
return request.param #固定写法,传入参数和变量:request.param
def test_prepare(prepare_data): #函数名为变量传入,拆包,把参数进行传入
print('参数传递%s'%prepare_data)
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture_param.py'])
返回内容:
test_fixture_param.py::test_prepare[1] 参数传递1
test_fixture_param.py::test_prepare[2] 参数传递2
test_fixture_param.py::test_prepare[4] 参数传递4
test_fixture_param.py::test_prepare[linda] 参数传递linda
4.多个fixture之间的传递
1、fixture传递,直接传入变量,就先执行website,在执行login
import pytest
@pytest.fixture()
def login(website): #fixture传递,直接传入变量,就先执行website,在执行login
print('登录用户名和密码')
@pytest.fixture()
def website():
print('打开浏览器')
def test_search():
print('用例1,不需要登录也可以进行搜索')
def test_cart(login):
print('用例2,需要登录才可以进行加入购物车')
def test_pay(login):
print('用例3,需要登录才可以进行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回内容:
test_fixture.py::test_search 用例1,不需要登录也可以进行搜索
test_fixture.py::test_cart 打开浏览器
登录用户名和密码
用例2,需要登录才可以进行加入购物车
test_fixture.py::test_pay 打开浏览器
登录用户名和密码
用例3,需要登录才可以进行支付
2、fixture传递,pytest.fixture(autouse=True),表示所有的test_xx用例都会执行这个 fixture传递。
import pytest
@pytest.fixture(autouse=True)
def login():
print('登录用户名和密码')
@pytest.fixture()
def website():
print('打开浏览器')
def test_search():
print('用例1,不需要登录也可以进行搜索')
def test_cart(website):
print('用例2,需要登录才可以进行加入购物车')
def test_pay(website):
print('用例3,需要登录才可以进行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回结果:
test_fixture.py::test_search 登录用户名和密码
用例1,不需要登录也可以进行搜索
test_fixture.py::test_cart 登录用户名和密码
打开浏览器
用例2,需要登录才可以进行加入购物车
test_fixture.py::test_pay 登录用户名和密码
打开浏览器
用例3,需要登录才可以进行支付
3、fixture传递,只针对特定的test_xx方法进行参数传递,可以通过标记的方式进行操作。
核心:@pytest.mark.usefixtures("login")
import pytest
@pytest.fixture()
def login():
print('登录用户名和密码')
def test_search():
print('用例1,不需要登录也可以进行搜索')
def test_cart():
print('用例2,需要登录才可以进行加入购物车')
@pytest.mark.usefixtures("login")
def test_pay():
print('用例3,需要登录才可以进行支付')
if __name__ == '__main__':
pytest.main(['-s','-v','test_fixture.py'])
返回结果:
test_fixture.py::test_search 用例1,不需要登录也可以进行搜索
test_fixture.py::test_cart 用例2,需要登录才可以进行加入购物车
test_fixture.py::test_pay 登录用户名和密码
用例3,需要登录才可以进行支付