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.
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: