1.在进行接口关联时,一般很多个接口共用一个上行接口(例如)登录,可以使用fixture定义一个测试夹具,将登录的接口写在框架的conftest.py文件中:
@pytest.fixture(scope="session")
def login_fixture():
keyword = Keyword()
url = "http://xxxxxxxxxxx/s=api/user/login"
pub_params = {"application": "app", "application_client_type": "weixin"}
data = {"accounts": "xxxxx", "pwd": "xxxxxx"}
response = keyword.request_post(url=url, params=pub_params, data=data)
token = jsonpath.jsonpath(response.json(), "$..token")[0]
return token
在需要用到登录接口的测试用例中,将这个夹具当做参数传入用例函数中,将夹具函数返回的值赋给变量token_value,在需要的地方直接引用这个变量
@allure.title("xxxxxxx")
def test_02_addcart3(login_fixture):
token_value = login_fixture
url_cart = "http://xxxxxxxxxx?s=api/cart/save"
pub_params = {"application": "app", "application_client_type": "weixin", "token": token_value}
data1 = {
"goods_id": "11",
"spec": "",
"stock": 5}
response = keyword.request_post(url=url_cart, params=pub_params, json=data1)
return response.json()
注:运行方式选择:
1.一定要使用pytest框架的入口执行文件,才能在执行用例的过程中,自动将夹具应用
2.如果选择其他执行方式,如unittest,就会报错为:
TypeError: test_02_addcart3() missing 1 required positional argument: 'login_fixture'
若报错信息为:AttributeError:"str"object Has no attribute "iter_parents",可以试着降低稳定版的pytest版本重试
标签:框架,url,fixture,token,pytest,params,login From: https://www.cnblogs.com/tyl-bok/p/18314854