25/06/2021
Recently (2021-06-25), a remote conference called Front-End Test Fest 2021 was held, conducted by Applitools and Cypress. One of the presentations focused on good practices for writing tests (link in the sources), and today’s post is an excerpt from that presentation.
Writing tests should not be done in the same way as writing code. Simplicity should take precedence, even at the cost of code duplication. This means that tests, if possible, should not rely on additional abstractions and should have a flat structure (without nesting).
The description of the test should clearly present its intentions. To achieve this, three conditions should be met in the description:
-> Arrange = setting up the scenario (variables, mocking, etc.)
-> Act = performing actions that lead to the final result
-> Assert = checking the execution of the scenario
Each test should have its own isolated environment and should not depend on the outcome of a preceding test. This means that tests should not be interdependent or linked in any way, and the data environment should be the same for each test.
One element of test readability is the use of realistic data (rather than placeholders like “Foo” and “Bar”). This allows for a better understanding of the connection to the tested product.
Selectors that are prone to change should not be used. Instead, it is best to rely on properties that are visible or relevant to the user (e.g., using cy.contains()) or assign test attributes like data-cy to the tested object.
Instead of arbitrarily waiting for a test task to complete (which will often depend on an HTTP request), it is better to wait for a positive response from that request.
cy.wait('@request')
.its('response.statusCode')
.should('equal', 200);Source: