ScalaTest中BeforeAndAfter和BeforeAndAfterEach有什么区别

What is the difference of BeforeAndAfter and BeforeAndAfterEach in ScalaTest

查看文档,两者似乎非常相似 (BeforeAndAfterEach BeforeAndAfter)。它们之间的核心区别是什么。在什么情况下应该使用其中一个而不是另一个(并且可能另一个甚至不起作用。)

顾名思义 BeforeAndAfter 将 运行 一次(在开始和结束时)用于测试 class。 BeforeAndAfterEach 将在 class.

中每个测试用例前后 运行

使用 BeforeAndAfter:假设您希望为 运行 测试创建一个内存数据库,您将在 运行 测试用例之前创建它,然后您将清除运行所有测试用例后的记忆。

使用BeforeAndAfterEach:假设你有一些缓存值,每个测试用例需要不同的值运行,在这种情况下,你可以在每个测试用例之前更改该缓存的值,并且运行单次测试后清除它。

您可以从 scalatest 文档中找到所需的所有信息。

TL;DR:两者具有相同的功能。 BeforeAndAfter更简洁,而BeforeAndAfterEach更可堆叠。

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.

我应该使用 BeforeAndAfter 还是 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.

我应该使用 BeforeAndAfter 还是 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。我看到有一个常见的错误,一个人会为每个测试执行而另一个人不会。还有另一个特征叫做 BeforeAndAfterAll.