Keeping your Test Data clean with beforeEach and afterEach

How to use the beforeEach and afterEach functions in Playwright to ensure that test data is clean and consistent.
Written by
Ben Fellows
Published on
January 23, 2023

Testing is an essential part of software development, and one of the most critical aspects of testing is ensuring that the test data is clean and consistent. Without clean test data, tests can be unreliable and produce inaccurate results. In this blog post, we will discuss how to keep your test data clean with the use of beforeEach and afterEach in Playwright.

Please note that this blog post assumes that you do not have a script in place to seed your database. In an ideal scenario, it is always recommended to have a script that seeds your database with test data, rather than having to rely on the methods discussed in this blog post. There are several tools available, such as Sequelize, that can be used to easily seed your database with test data. This can save a lot of time and effort in setting up test data and ensure that your tests are reliable and consistent. However, if you don't have access to such tools or scripts, the methods discussed in this blog post can be used as a fallback option to keep your test data clean.

Quick Reminder about Test Case Isolation

Playwright is built on the principle of test case isolation, which means that each test should be independent of other tests and should not affect the results of other tests. This is important because it ensures that tests are reliable, that any issues that arise can be easily identified and fixed, and also allows for parallelization. Parallelization is a technique of running multiple tests simultaneously, which can significantly improve the speed of your test suite. When tests are independent of each other, it's easier to run them in parallel without the risk of them affecting each other's results.

One way to achieve test case isolation is by using the beforeEach function to create all the data necessary for the test. This allows you to set up the test data in a known state before each test and ensures that the test data is clean and consistent. Additionally, you can use the afterEach function to delete any data created during the test. This ensures that the test data is always clean and consistent after each test, regardless of whether the test passed or failed.

The beforeAll function is used to run code before all the tests in a test suite are executed. This is useful for setting up any resources or data that are needed for all the tests in the suite. For example, you might use beforeAll to connect to a database or start a web server that the tests rely on.

The afterEach function is used to run code after each individual test in a test suite. This is useful for cleaning up any resources or data that were used during the test. For example, you might use afterEach to delete a user from the database or clear the browser's local storage after each test.

By using these options, you can ensure that your tests are always executed in a known state and that any resources or data that are used during the tests are properly handled. This can help to improve the reliability and accuracy of your tests, and make it easier to identify and fix any issues that arise.

Setting up Data with BeforeEach

Setting up data with the beforeEach function is a common practice in test automation. It allows you to set up the test data in a known state before each test, ensuring that the test data is clean and consistent. This is particularly useful when working with databases, as it allows you to reset the data to a known state before each test.

For example, let's say you are writing tests for an application that uses a database to store customer information. You might use the beforeEach function to create a new customer in the database before each test. This can be done by executing a SQL query to insert a new row into the customer table before each test.

You can also use the beforeEach function to set up other types of data, such as creating a new user in a web application or navigating to a specific page on a website.

Additionally, you can use the beforeEach function to set up any necessary environment, such as setting variables, or configuring test settings. This can help to make your tests more efficient and reliable.

In summary, using the beforeEach function to set up data is a powerful technique in test automation. It allows you to set up the test data in a known state before each test, ensuring that the test data is clean and consistent, and it can also be used to set up other types of data and environments that are necessary for the tests. This can help to improve the reliability and accuracy of your tests, and make it easier to identify and fix any issues that arise. 

Download our Automation ROI Formula

Tearing Down With AfterEach

In test automation, the afterEach function is executed after every test, regardless of whether the test passed or failed. This means that it is important to ensure that the code inside the afterEach function does not cause any issues if the test fails. One way to avoid this problem is by using boolean checkpoints in your tests, as we discussed earlier.

By adding boolean checkpoints to your tests, you can ensure that the test data is only cleaned up if the test has passed and the data has been created successfully. For example, you might use a boolean variable to track whether the customer was created in the test before deleting it in the afterEach function.

In this example, the test will only pass if the customer is successfully created in the database. If the test fails, the afterEach function will not run, and the customer will not be deleted from the database.

Another way to handle this is by using if statements in the afterEach function that check whether the data is present before trying to delete it.

By using if statements in the afterEach function that check whether the data is present before trying to delete it, you can ensure that your test data is only cleaned up if the test has passed and the data has been created successfully. This will prevent any errors related to trying to delete data that wasn't created. This approach works similarly as the boolean checkpoint approach, but it has the advantage of checking the data directly, instead of relying on a variable.

Both the boolean checkpoint approach and the if statement approach are effective ways to ensure that your test data is only cleaned up if the test has passed and the data has been created successfully. By using these methods in your afterEach function, you can prevent any errors related to trying to delete data that wasn't created and ensure that your test data is always clean and consistent. This can help to improve the reliability and accuracy of your tests, and make it easier to identify and fix any issues that arise.

Database, API, & UX/UI

Keeping your test data clean can be done at different levels, such as the database level, API level, and UX/UI level. Each level has its own set of tools and techniques that can be used to ensure that your test data is always clean and consistent.

At the database level, you can use SQL queries or ORM tools such as Sequelize to create, read, update, and delete data in a controlled and consistent manner. For example, you can use the beforeEach function to clear the customer table before each test, ensuring that the data is in a known state before the test is run. This can be done by executing a SQL query to delete all the rows from the customer table before each test, like so:

At the API level, you can use the API to create, read, update and delete data in a controlled and consistent manner. For example, you can use the beforeEach function to create a new customer before each test and delete it after each test.

At the UX/UI level, you can use the user interface to create, read, update and delete data in a controlled and consistent manner. For example, you can use the beforeEach function to create a new customer before each test and delete it after each test.

By using these methods at the database level, API level, and UX/UI level, you can ensure that your test data is always clean and consistent, regardless of the level of the application you are testing. This can help to improve the reliability and accuracy of your tests and make it easier to identify and fix any issues that arise.

Download our Automation ROI Formula

Conclusion

In conclusion, keeping your test data clean and consistent is crucial for ensuring the reliability and accuracy of your tests. The beforeEach and afterEach functions in Playwright provide a simple and effective way to maintain test data by setting it up in a known state before each test and cleaning it up after each test. Additionally, the beforeAll and afterAll functions can be used for setting up and cleaning up resources or data needed for a test suite. By utilizing these functions, you can improve the reliability and accuracy of your tests, making it easier to identify and fix any issues that may arise. Remember that ideally, a script should be used to seed the database with test data as it can save time and effort in setting up test data, but in case it is not available, the methods discussed in this blog post can be a fallback option to keep your test data clean.

Free Quality Training
Enhance your software quality for free with our QA training and evaluation. Sign up now to boost your team's skills and product excellence!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.