Search code examples
scalascalatest

What is the difference of BeforeAndAfter and BeforeAndAfterEach in ScalaTest


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.)


Solution

  • 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 trait BeforeAndAfterEach 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 extend BeforeAndAfterEach 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, whereas BeforeAndAfter 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 where BeforeAndAfterEach 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 over BeforeAndAfter is that BeforeAndAfterEach. enables trait stacking. The main disadvantage of BeforeAndAfterEach compared to BeforeAndAfter is that BeforeAndAfterEach requires more boilerplate. If you don't need trait stacking, use BeforeAndAfter instead of BeforeAndAfterEach. If you want to make use of test data (the test name, config map, etc.) in your beforeEach or afterEach method, use trait BeforeAndAfterEachTestData 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.