Clean app state after running an E2E test? #336
Replies: 1 comment 2 replies
-
Following Cypress's best practices, app state clean up should be done before running any tests, as the after() and afterEach() hooks are not executed if any step of the tests fails. I would create a resetDatabase() or cleanDatabaseData() and a create/loginAdminUser()/loginGuestUser() commands before every test, that way, we reset the app state between tests. In case we want to debug the tests when they fail, we can run it again with those commands commented. I see two ways of implementing this:
I like the second option, as it removes the need to write the same code in every single test, but, at the same time, it hinders readability. Overall I think cleaning the app state after every test is the right thing to do regardless of the way it is implemented. |
Beta Was this translation helpful? Give feedback.
-
Hi, we have been discussing whether it is a good idea or not to clean the stage changes made by a test.
For example, if you have an E2E test for user registration, you can leave a newly created user or delete it after running the test.
We are using random values for the tests, so they are isolated, and you can run them many times. For example, the test to sign up a new user always uses new random data for the user. If the test fails and you rerun it, you will not have a false negative because the user already exists.
I was discussing this topic with @Wolfremium13 on different PRs. We agreed that cleaning the state after running the test was a good practice. If you need to debug why a test is failing, you can comment the code cleaning the state after running the test.
@mario-nt mentioned that cleaning the state after running a test is actually considered a bad practice on the Cypress documentation: https://docs.cypress.io/guides/references/best-practices#Using-after-Or-afterEach-Hooks
I would differentiate the two environments we usually use for running tests:
I think this discussion only applies to the second one, which is the one we use for E2E testing.
I just want to open the discussion to track pros and cons. I personally think it's better to clean the state (as much as you can). It does not matter if sometimes a test fails and the state is not clean in that case. In fact, in that case, you would prefer to rerun the test without cleaning the state to check what happened.
Links
cc @da2ce7 @mario-nt
1 vote ·
Beta Was this translation helpful? Give feedback.
All reactions