Conditional Testing

Learn about the potential problems with conditional testing and some possible solutions.

Conditional testing can be described in this simple pseudo-code:

If A
    Do B
Else
    Do C

We usually need these kinds of tests when we want to:

  • Test things differently based on the presence of an element.
  • Cope with A/B testing in the background.
  • Do dynamic testing based on the content of our app.

The problem with conditions in tests

There is a fundamental problem with these types of tests. While we can easily write them, they often lead to flaky tests and random failures, making tracking these failures harder.

We can only write conditions once the state of the application has settled. However, it is often impossible to know when that is, especially with highly dynamic SPAs. This instability can cause the previously mentioned random failures and flakes.


Possible solutions

There are possible solutions to the problem, but it’s important to mention that they will not be bulletproof. They can only reduce flakes. But if we need to have conditions in our test, it will never fully eliminate them.

Being explicit

One way we can fight flakes and random errors is by being explicit.

Imagine our site has multiple tests running that can ultimately alter the result of our E2E tests. In this case, we want to be explicit about which variation of the A/B test the user will receive, whether it be by:

...