import pytest,requests,jsonpath params = { "application": "app", "application_client_type": "weixin", } header ={ "application": "app", "application_client_type": "weixin", "token":"token_list" } data = { "accounts":"huace_xm", "pwd":"123456", "type":"username" } def test_01(): res = requests.post \ (url="http://shop-xo.hctestedu.com/index.php?s=api/user/login", params=params, data=data) print(res.text) # print(res.json()["data"]["token"]) token_list = jsonpath.jsonpath(res.json(), "$..token") print(token_list) # 转成json数据 --字典的写法 def test_02(): data2 = { "id": "12" } res1 = requests.post \ (url="http://shop-xo.hctestedu.com/index.php" "?s=api/goods/favor&token=" + token_list[0], params=params, data=data2) print(res1.text)
时间: 2023-08-07 17:11:18 浏览: 41
这段代码是一个使用Python的pytest框架进行接口测试的示例。这个示例中,我们首先定义了三个变量`params`、`header`和`data`,分别用于存储请求的URL参数、请求头和请求体数据。然后我们定义了两个测试用例`test_01`和`test_02`,分别用于测试用户登录接口和商品收藏接口。
在`test_01`中,我们使用`requests.post`方法发送了一个POST请求到用户登录接口,并传入了URL参数、请求头和请求体数据。然后我们打印出了响应结果的文本内容,并使用`jsonpath`模块从响应结果中提取了`token`值。最后,我们将`token`值存储到了`header`变量中,以便在后续的测试用例中使用。
在`test_02`中,我们使用`requests.post`方法发送了一个POST请求到商品收藏接口,并传入了URL参数、请求头和请求体数据。注意,在URL中我们使用了`token_list[0]`来动态地传入之前登录接口返回的`token`值。最后,我们打印出了响应结果的文本内容。
相关问题
import pytest,requests,jsonpath params = { "application": "app", "application_client_type": "weixin", } header ={ "application": "app", "application_client_type": "weixin", "token":"token_list" } data = { "accounts":"huace_xm", "pwd":"123456", "type":"username" } def test_01(): res = requests.post \ (url="http://shop-xo.hctestedu.com/index.php?s=api/user/login", params=params, data=data) print(res.text) # print(res.json()["data"]["token"]) token_list = jsonpath.jsonpath(res.json(), "$..token") print(token_list) # 转成json数据 --字典的写法 def test_02(): data2 = { "id": "12" } res1 = requests.post \ (url="http://shop-xo.hctestedu.com/index.php" "?s=api/goods/favor&token=" + token_list[0], params=params, data=data2) print(res1.text)这个代码报错原因为什么是E NameError: name 'token_list' is not defined
这个代码报错是因为`token_list`变量只在`test_01`函数中被定义,而在`test_02`函数中却没有被定义。因此,当`test_02`函数中的代码尝试使用`token_list`变量时,Python会抛出一个`NameError`异常,提示该变量未被定义。
要解决这个问题,可以将`token_list`变量定义为全局变量,使它在整个程序中都可以被访问。具体来说,可以将其定义在函数外部,例如:
```
import pytest
import requests
import jsonpath
params = {
"application": "app",
"application_client_type": "weixin",
}
header = {
"application": "app",
"application_client_type": "weixin",
"token": "token_list",
}
data = {
"accounts": "huace_xm",
"pwd": "123456",
"type": "username",
}
# 定义全局变量
token_list = []
def test_01():
global token_list # 声明使用全局变量
res = requests.post(
url="http://shop-xo.hctestedu.com/index.php?s=api/user/login",
params=params,
data=data
)
print(res.text)
token_list = jsonpath.jsonpath(res.json(), "$..token")
print(token_list)
def test_02():
global token_list # 声明使用全局变量
data2 = {
"id": "12"
}
res1 = requests.post(
url="http://shop-xo.hctestedu.com/index.php?s=api/goods/favor&token=" + token_list[0],
params=params,
data=data2
)
print(res1.text)
```
在这个例子中,我们将`token_list`变量定义为全局变量,并在`test_01`和`test_02`函数中都声明使用全局变量。这样,在`test_01`函数中赋值给`token_list`变量的值就可以在`test_02`函数中使用了。
import pytest class test100: def test1(self): assert 1==1 if __name__=='_main_': pytest.main([__name__])
这段代码中,你定义了一个名为test100的类,并在其中定义了一个名为test1的方法。在test1方法中,你使用了pytest库的assert语句来判断1是否等于1。然后,你使用了一个条件判断语句,检查当前脚本是否作为主程序运行。如果是主程序运行,就调用pytest库的main函数来执行当前模块中的所有测试用例。
需要注意的是,正确的条件判断应该是`if __name__=='__main__':`,而不是`if __name__=='_main_':`。双下划线在两边都要有。此外,你需要将代码放在一个.py文件中才能运行。