cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.8k stars 3.17k forks source link

Add real Global Before and After hooks #29432

Open jprealini opened 5 months ago

jprealini commented 5 months ago

What would you like?

Currently I am facing a scenario where, to setup and teardown test users, I depend on a Cypress plugin developed by our company that uses custom commands to run API requests (using cy.request).

The expectation is that the test users setup will occur ONCE when the test run begins, and the teardown (actually removing the users from db) will happen ONCE after the whole test run has finished

I was convinced that the before and after hooks we write in the support file are executed only ONCE before and after the whole test run, but I learned that they run before and after each spec (which Cypress considers a suite)... I am not sure then about the difference between those hooks and the "before and after" hooks you add in the specs themselves.. wouldn't they do the same?

It would be awesome to provide with real Global Before and Global After hooks for easy test data setup and teardown.

Why is this needed?

It would be easier to run certain code that relies on cypress (cy commands, even custom commands, or cy.request) only ONCE starting the test run and ONCE finishing the test run, instead of having to write "cypress agnostic" code to be called from "before:run" or "after:run" events.

Other

No response

jennifer-shehane commented 5 months ago

There is a before:run and after:run API as part of setupNodeEvents that does this.

I'm not sure if we could hook into this once a spec has begun, so that you could run Cypress tests.... it would require quite a bit of rework anyway.

jprealini commented 5 months ago

Thanks for the input @jennifer-shehane

Yeah, I know I can use those apis. The problem is that the whole setup and cleanup processes in our scenario make use of a lot of cypress commands, which blocks us from being able to call them from there.

It would be awesome if we could have a true global before hook (one that runs just one at the beginning of the test run) and a true global after hook (that runs just once, after the whole test run has completed)... Are there any limitations that would prevent Cypress to provide such functionality?

Thanks again.

jprealini commented 5 months ago

NOTE: I know that using the after or afterEach hooks to cleanup state or test data is an anti pattern. The problem we face is that we are required by dba's to cleanup every test data we create in the lower environments databases for our tests as soon as the test runs are completed.

We could (and that's what we are doing) delete the users after each spec is completed. The problem with that is that we are creating between 4 and 10 users depending on the run type, and creating that amount of users takes a lot of time... If we have to create them before each spec, with 20 specs we will have more than 40 minutes taken only for test users setup and cleanup.

That's why we decided to create a set of users on startup, use them across all our test run, and clean them up when we are done...

Hope that makes sense.