preston.m.price
preston.m.price

Reputation: 666

JMeter assertion modularity (can I re-use assertions?)

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:

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!

Upvotes: 4

Views: 817

Answers (2)

preston.m.price
preston.m.price

Reputation: 666

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.

Upvotes: 0

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34536

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.

Upvotes: 0

Related Questions