assertions

By default, JMeter marks any HTTP request with a fail response code (4xx or 5xx) as failed, which allows you to easily identify when some request unexpectedly fails.

But in many cases, this is not enough or desirable, and you need to check for the response body (or some other field) to contain (or not) a certain string.

This is usually accomplished in JMeter with the usage of Response Assertions, which provides an easy and fast way to verify that you get the proper response for each step of the test plan, marking the request as a failure when the specified condition is not met.

example - 1:

In this example we assert that the response contains the text “var”

from pymeter.api.config import TestPlan, ThreadGroupSimple
from pymeter.api.samplers import HttpSampler
from pymeter.api.timers import ConstantTimer
from pymeter.api.assertions import ResponseAssertion

timer = ConstantTimer(2000)
ra = ResponseAssertion().contains_substrings("var")
http_sampler = HttpSampler("Echo", "https://postman-echo.com/get?var=1", timer, ra)
thread_group = ThreadGroupSimple(1, 1)
thread_group.children(http_sampler)
test_plan = TestPlan(thread_group)
stats = test_plan.run()
class pymeter.api.assertions.BaseAssertion

Bases: ThreadGroupChildElement

base class for all assertions

children(*children)

adds children to element Example 1:

    from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
    from pymeter.api.samplers import HttpSampler

    http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
    http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

    thread_group_setup = SetupThreadGroup()
    thread_group_setup.children(http_sampler1, http_sampler2)

Example 2:
from pymeter.api.config import TestPlan, ThreadGroupWithRampUpAndHold, SetupThreadGroup, TeardownThreadGroup
from pymeter.api.samplers import HttpSampler

http_sampler1 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=1")
http_sampler2 = HttpSampler("echo_get_request", "https://postman-echo.com/get?var=2")

thread_group_setup = SetupThreadGroup()
thread_group_setup.children(http_sampler1)
thread_group_setup.children(http_sampler2)
class pymeter.api.assertions.ResponseAssertion

Bases: BaseAssertion

Assertion of the response element

contains_substrings(*strings_to_look)

assert that the response contains string the given strings