4
votes

I am working on a test plan for our REST web application and we have several common test types which have common criteria we want to test for. For example, when creating entities through the API we have a common set of expectations for the JSON response; id should be set, created date should be set, etc.

Now, I would like to model my plans like this:

  • Thread Group
    • Users (Simple Controller)
      • User Create Tests (Simple Controller)
        • Create Test 1 (Sampler)
        • Create Test 2 (Sampler)
        • Create Test 3 (Sampler)
        • Common Creation Asserts (Module Controller)
      • User Delete Tests (Simple Controller)
        • Samplers...
        • Common Delete Asserts (Module Controller)
    • Events (Simple Controller)
      • Event Create Tests (Simple Controller)
        • Samplers...
        • Common Creation Asserts (Module Controller)
      • Event Delete Tests (Simple Controller)
        • Samplers...
        • Common Delete Asserts (Module Controller)
  • Thread Group for common assertions (disabled)
    • Common Creation Assertions (Simple Controller)
      • BSF Assertion 1
      • BSF Assertion 2
      • BSF Assertion 3
    • Common Delete Assertions (Simple Controller)
      • Asserts...

Now, I understand how the scoping works and that if I placed assertions where the BOLDed module controllers are they would be invoked for each sampler. However, I'd rather not have to copy-paste-maintain numerous copies of the same assertions in each of these locations. Hence, why I want a way to define assertions once, and invoke where appropriate.

However, with this approach, the ACCENTed assertions placed in the common simple controllers are never invoked (confirmed by using a BSF assertion with logging messages). If I place an additional sampler in the common assertions simple controller it is invoked. But only a single time.

I'm using JMeter 2.12 but have confirmed that JMeter 2.8 behaves the same way.

So, how can I use JMeter to define assertions once, and re-use them anywhere?

Thanks!

2

2 Answers

0
votes

There is no way to do this.

You can try factoring by using Variables within Assertions, thus if it's a Response Assertion you will factor out this.

0
votes

I ended up getting creative. Using JSR223 assertions in Javascript I've accomplished what I wanted. This is a natural fit because all the response data I want to test is in JSON, YMMV.

In User Defined Variables I define the tests I want to perform using Javascript.

Tests like:

TEST_JSON:

try
{
    eval('var obj = ' + prev.getResponseDataAsString());
} catch(e)
{
    setFailed();
}

TEST_RESULT_SUCCESS

if(obj.status != "success")
{
    setFailed();
}`

Then in the assertion(s) I can do something like:

eval(vars.get("TEST_JSON"));
eval(vars.get("TEST_RESULT_SUCCESS"));

And I don't have to re-write tests over and over and over.

I even have some a some utility functions that I can add to my assertion by doing

eval(vars.get("TEST_UTIL"));

which allows me to print additional logging from my assertions if I desire.