如何在 python 脚本中解析和打印来自 pymeter 的 API 响应和错误代码?
PyMeter 是 jmeter 的 python 版本。 (pymeter 帮助文档 - https://pymeter.readthedocs.io/en/latest/api.html )
我正在尝试获取 API 的性能统计数据 - https:// reqres.in/api/users?page=2 它给出以下响应。我可以使用 pymeter 采样器执行 api,但是无法找到任何有关如何解析 API 响应的帮助文档。
{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
这是我的示例脚本 -
from unittest import TestCase
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold
from pymeter.api.samplers import HttpSampler
from pymeter.api.reporters import HtmlReporter
from pymeter.api.postprocessors import JsonExtractor
from pymeter.api.assertions import ResponseAssertion
class TestTestPlanClass(TestCase):
def __init__(self, *args, **kwargs):
super(TestTestPlanClass, self).__init__(*args, **kwargs)
print("TestTestPlanClass")
@classmethod
def setUpClass(cls):
print("setUpClass")
def test_case_1(self):
json_extractor = JsonExtractor("variable", "args.data")
# create HTTP sampler, sends a get request to the given url
ra = ResponseAssertion().contains_substrings("data")
http_sampler = HttpSampler("echo_get_request", "https://reqres.in/api/users?page=2", json_extractor)
# create html reporter
html_reporter = HtmlReporter()
# create a thread group that will rump up 10 threads in 1 second and
# hold the load for additional 10 seconds, give it the http sampler as a child input
thread_group_main = ThreadGroupWithRampUpAndHold(1, 1, 1, http_sampler)
# create a test plan with the required thread group
test_plan = TestPlan(thread_group_main, html_reporter)
# run the test plan and take the results
stats = test_plan.run()
self.assertLess(stats.sample_time_99_percentile_milliseconds, 2000)
@classmethod
def tearDownClass(cls):
print("tearDownClass")
你可以在 PyMeter 中使用
JsonExtractor
后处理器解析 JSON 响应,然后使用响应断言或自定义逻辑访问提取的值。以下是如何执行此操作的方法:
1. 使用 JsonExtractor 提取值:
你已经在脚本中使用了
JsonExtractor
,但让我们对其进行调整以提取所需的值。例如,要提取第一个用户的电子邮件地址,可以使用以下 JSON 路径:
$.data[0].email
。
json_extractor = JsonExtractor("user_email", "$.data[0].email")
这将创建一个名为“user_email”的变量,并在其中存储提取的值。
2. 在响应断言中访问提取的值:
你可以使用
${}
语法在响应断言中访问提取的变量。例如,要断言第一个用户的电子邮件地址包含“reqres.in”,可以使用以下代码:
ra = ResponseAssertion().contains_substrings("${user_email}")
3. 在自定义逻辑中访问提取的值:
你也可以在
test_case_1
方法中访问提取的变量,例如:
# ... your existing code ...
stats = test_plan.run()
# Access the extracted value
extracted_email = stats.vars.get("user_email")
print(f"Extracted email: {extracted_email}")
# ... your assertion logic ...
完整脚本:
from unittest import TestCase
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold
from pymeter.api.samplers import HttpSampler
from pymeter.api.reporters import HtmlReporter
from pymeter.api.postprocessors import JsonExtractor
from pymeter.api.assertions import ResponseAssertion
class TestTestPlanClass(TestCase):
# ... your existing code ...
def test_case_1(self):
# Extract the email of the first user
json_extractor = JsonExtractor("user_email", "$.data[0].email")
# ... your existing code ...
# create a thread group ...
thread_group_main = ThreadGroupWithRampUpAndHold(1, 1, 1, http_sampler)
# ... your existing code ...
# run the test plan and take the results
stats = test_plan.run()
# Access the extracted value
extracted_email = stats.vars.get("user_email")
print(f"Extracted email: {extracted_email}")
# Assert the extracted email contains "reqres.in"
self.assertIn("reqres.in", extracted_email)
# ... your other assertion logic ...
# ... your existing code ...
错误代码:
要获取 API 响应的错误代码,可以使用
ResponseAssertion
并检查响应代码是否与预期值匹配。例如,要断言响应代码为 200(OK),可以使用以下代码:
ra = ResponseAssertion().assert_status_code(200)
你还可以使用
stats.response_code
属性在自定义逻辑中访问响应代码。