Reputation: 8651
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.)
Upvotes: 4
Views: 1605
Reputation: 8539
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
BeforeAndAfter
when 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
BeforeAndAfter
provides 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 traitBeforeAndAfterEach
instead.The advantage this trait has over BeforeAndAfterEach is that its syntax is more concise. The main disadvantage is that it is not stackable, whereas
BeforeAndAfterEach
is. I.e., you can write several traits that extendBeforeAndAfterEach
and 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,BeforeAndAfterEach
allows you to access the config map and test name via the TestData passed to its beforeEach and afterEach methods, whereasBeforeAndAfter
gives you no access to the config map.
BeforeAndAfterEach
recommended usage:
Use trait
BeforeAndAfterEach
when 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 whereBeforeAndAfterEach
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 BeforeAndAfterEach
perspective):
The main advantage of
BeforeAndAfterEach
overBeforeAndAfter
is thatBeforeAndAfterEach
. enables trait stacking. The main disadvantage ofBeforeAndAfterEach
compared to BeforeAndAfter is thatBeforeAndAfterEach
requires more boilerplate. If you don't need trait stacking, useBeforeAndAfter
instead ofBeforeAndAfterEach
. If you want to make use of test data (the test name, config map, etc.) in your beforeEach or afterEach method, use traitBeforeAndAfterEachTestData
instead.
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
.
Upvotes: 7
Reputation: 2686
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.
Upvotes: 0