We can divide our test case into three to four logical parts.
- Setup - Action to be performed before actual test part. e.g navigate to
a section via navbar. The common part of the
setup
can be added in before
or beforeEach
.
- Exercise - The actual test part e.g. type text and click on submit
button.
- Assertion - Verifying the exercise part e.g. asserting the success
message.
- Teardown - This is done after performing assertion. This might not be
needed for each of the test cases. Basically in teardown, we restore the
state of the app.
An example could be to write a test case to add a customer.
All the test scenarios (both positive and negative) for a functionality can be
written in a single test case. We can separate the test scenarios by a comment
and empty line.
Reasons to keep all the scenarios in a single test case
- To reduce the overall test execution time. Most of the applications which we
work on are powered by front-end frameworks like React.js. We can avoid unnecessary page refreshes for testing various scenarios, resulting in shorter testing times.
- Cypress dashboard treats each
it
block as a separate test. Cypress
dashboard service charges for each test. If we have less tests, we will be
charged less.
For example, Suppose we want to create a new task, we can write a test case covering all the
scenarios. We can start with the cancel functionality, validations, create a new task, edit
a new task and delete a new task.
Here we wrote only one test, namely,
should verify new task operations
which contains all the test scenarios.