有条件地执行断言

Conditionally perform assertion

我想仅在满足特定条件(即变量和参数具有特定值)时才对采样器执行断言。如果条件不满足,应该忽略断言,而不是失败。

我有哪些选择?

if 控制器似乎不起作用,因为它显然要求采样器(始终应调用)也在其范围内。

我只能想到使用 JSR223 Assertion which allows you executing arbitrary Groovy 提供最大灵活性的代码。

下面是一个简单的代码示例:

if (vars.get('foo') == 'bar') {                            // execute only if JMeter Variable ${foo} is equal to "bar"
    if (!prev.getResponseDataAsString().contains('baz')) { // if there is no "buz" word in the response
        assertionResult.setFailure(true)                   //fail the sampler
        assertionResult.setFailureMessage('Failed to find word "baz" in the response')
    }
}

查看 Scripting JMeter Assertions in Groovy - A Tutorial 文章了解更多信息。