Tarun
Tarun

Reputation: 3496

Inducing sane multiple assertions with integration tests

I have came across many resources which talk about using/not using multiple assertions during unit testing. But while writing UI level automation integration tests I end up in doing many assertion in one test, which does not seem very bad idea to me, especially when I use soft assertions which fail only during tear down and reporting all assertion failures in a test method, instead of limiting it to one report per test.

One such scenarios is filling a form having 10 fields (text box, drop down etc). Coming back to the form and verifying all entered values are available. What I don't like with my tests is, it is filled with many assertions. I want to assert all of these values but yet want my tests to look clean and not like -

 public void testMethod() {
  // Some operation here
  softAssert("verification failed for field 1, expected value:" +value, isValuePresent(value));
  softAssert("verification failed for field 2, expected value:" +value, isValuePresent(value));
  softAssert("verification failed for field 3, expected value:" +value, isValuePresent(value));
  // Some more assertions here
}

I could extract these assertions to a different method but then I feel that assertions should be kept in test methods. to make it clear what is being tested in a test method.

Is just a trivial wishy washy feeling I have and such design of tests is justified? Or I could make design enhancements in my test methods.

Upvotes: 0

Views: 325

Answers (2)

user949300
user949300

Reputation: 15729

I often call toString() on some of the results and assertEquals to the correct result. If your underlying objects implement a reasonable toString (or a toXML etc.) That simplifies the test code. But it's less robust to future changes.

Upvotes: 1

dMb
dMb

Reputation: 9357

you can do what I'd call "assertion by example" which is simply an assertion at the form level. It would look something like this:

public void testMethod() {
  Form expected = new Form()
                    .field1('value1')
                    .field2('value2')
                    .field3('value3')
                    .field4('value4')

  Form result = someFormOperation();

  softAssert(expected, result);

}

Upvotes: 3

Related Questions