Multiple vs single asserts per test in selenium? Multiple vs single asserts per test in selenium? selenium selenium

Multiple vs single asserts per test in selenium?


Tests should test the system and user behavior not just the assertion .

you can change your tests as ("generic example") :

let user and summarypage be the pageobjects so:

summary class:

class summary(){public static expectedDetails = ["something1", "something2"]function getDetails(){   return [self.claimSummaryPage.return_claim_payments_mosaic_text()]}}

now your test:

test("validate user can successfully login and vie claim summary"){      user.userlogins()   details = summary.getDetails()   assert(details).to.be.equal(summary.expectedDetails)   }

here instead of individually validaiting each string , we are saving to an array and comparing the resulting array and expected array

This is much cleaner approach. Don't add assertion in pageobject


Given that gui tests take much more time it would probably not be efficient to just have one assert in each test. The best would probably be to have a test suite where in which you execute one assert per test during the same run. I've also had experience where we implemented our own assert methods for the gui tests which caches the results from all asserts in the gui tests and goes through them in the end and fails the test if any of the cached assertions failed. This is due to the nature of the system we worked with at the time. Maybe this could be a way to solve it for you?

This works given that the assertions you do are not on anything that would cause an error if you continue even if the assertion fails, e.g. if a step in a process would fail.

Example:

my_assertion_cache = list()def assert_equals(a, b):    try:        assert a == b    except AssertionError:        # preferably add a reference to the locator where this failed into the message below        my_assertion_cache.append(f"{a} and {b} was expected to be equal")def run_after_each_test():    assert mylist == []


Generaly if your first assert fails, then others will not be executed in case where you have multiple assertions in one test.

On the other handIf you do not perform any new action in your test, like you are on page and you are checking some UI and do not perform any click, or select, or any new action, you can use multiple assertions.

Remember Auto test are used so you dont need to run tests manualy, and they can identify problem faster, and with more precision.This is why recomendation goes to the one assertion, one test.

So the question can be translated like this: Do I want to identify only one issue, or all possible issues with the auto tests?