简体   繁体   中英

What is the difference between Cypress before() hook and it() test

As it follows from Cypress documentation, it is "helpful to set conditions that you want to run before a set of tests". But why do we actually need to have it as a separate unit? We can use any it() for this aim is not it? Does before() have something special which differs it from it()?

before and beforeEach help you set up state and run something before each test. The goal is to not rely on state from a previous it statement. It statements will run but after the it statement concludes, Cypress will clear the cookies/storage/etc. The best example is having a login method and call it before each it block that requires being a logged-in user.

before() runs once before all tests in the block.

I suppose it's the same as writing in the first 'it' test, but 'it' tests can be skipped, removed or can fail, whereas before() will always be there.

https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Hooks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM