Looking at documentation, the two seem very similar (BeforeAndAfterEach BeforeAndAfter). What is the core difference between them. What are the situations when one of them should be used over the other (and possibly the other wouldn't even work.)
Looking at documentation, the two seem very similar (BeforeAndAfterEach BeforeAndAfter). What is the core difference between them. What are the situations when one of them should be used over the other (and possibly the other wouldn't even work.)
From scalatest documentation you can find all the info you need.
TL;DR: Both have the same functionality. BeforeAndAfter is more concise, while BeforeAndAfterEach is more stackable.
BeforeAndAfter recommended usage:
Use trait
BeforeAndAfterwhen you need to perform the same side-effects before and/or after tests, rather than at the beginning or end of tests. Note: For more insight into where BeforeAndAfter fits into the big picture, see the Shared fixtures section in the documentation for your chosen style trait.
Should I use BeforeAndAfter or BeforeAndAfterEach? (From BeforeAndAfter perspective)
Although
BeforeAndAfterprovides a minimal-boilerplate way to execute code before and after tests, it isn't designed to enable stackable traits, because the order of execution would be non-obvious. If you want to factor out before and after code that is common to multiple test suites, you should use traitBeforeAndAfterEachinstead.The advantage this trait has over BeforeAndAfterEach is that its syntax is more concise. The main disadvantage is that it is not stackable, whereas
BeforeAndAfterEachis. I.e., you can write several traits that extendBeforeAndAfterEachand provide beforeEach methods that include a call to super.beforeEach, and mix them together in various combinations. By contrast, only one call to the before registration function is allowed in a suite or spec that mixes in BeforeAndAfter. In addition,BeforeAndAfterEachallows you to access the config map and test name via the TestData passed to its beforeEach and afterEach methods, whereasBeforeAndAftergives you no access to the config map.
BeforeAndAfterEach recommended usage:
Use trait
BeforeAndAfterEachwhen you want to stack traits that perform side-effects before and/or after tests, rather than at the beginning or end of tests. Note: For more insight into whereBeforeAndAfterEachfits into the big picture, see the Shared fixtures section in the documentation for your chosen style trait.
Should I use BeforeAndAfter or BeforeAndAfterEach? (From BeforeAndAfterEach perspective):
The main advantage of
BeforeAndAfterEachoverBeforeAndAfteris thatBeforeAndAfterEach. enables trait stacking. The main disadvantage ofBeforeAndAfterEachcompared to BeforeAndAfter is thatBeforeAndAfterEachrequires more boilerplate. If you don't need trait stacking, useBeforeAndAfterinstead ofBeforeAndAfterEach. If you want to make use of test data (the test name, config map, etc.) in your beforeEach or afterEach method, use traitBeforeAndAfterEachTestDatainstead.
P.s. I see that there is a common mistake that one will execute for each test and the other won't. There is another trait for that called BeforeAndAfterAll.
As the name suggest
BeforeAndAfter will run once(at start and at the end) for a test class.
BeforeAndAfterEach will run before and after each test case in the class.
Use BeforeAndAfter: Suppose you want you create an in Memory database for the tests to run you will create it before running the test cases and then you will clear the memory after running all the test cases.
Use BeforeAndAfterEach: Suppose you have some cache value which requires different value for each test case to run, in that case you can change the value of that cache for before each test case and clear it after running a single test.