JUnit 最佳实践:每个 @Test 都有不同的装置

发布于 2024-08-31 00:11:31 字数 322 浏览 2 评论 0 原文

据我所知,有 @Before@BeforeClass,它们用于定义 @Test 的固定装置。但是,如果每个 @Test 需要不同的装置,我应该使用什么?

  • 我应该在中定义夹具吗 @Test
  • 我应该创建一个测试类吗 对于每个@Test

我在这里寻求最佳实践,因为我认为这两种解决方案都不干净。对于第一个解决方案,我将测试初始化​​代码。通过第二个解决方案,我将打破“每个类一个测试类”的模式。

I understand that there are @Before and @BeforeClass, which are used to define fixtures for the @Test's. But what should I use if I need different fixtures for each @Test?

  • Should I define the fixture in the
    @Test?
  • Should I create a test class
    for each @Test?

I am asking for the best practices here, since both solutions aren't clean in my opinion. With the first solution, I would test the initialization code. And with the second solution I would break the "one test class for each class" pattern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谎言 2024-09-07 00:11:31

提示:

  1. 忘记每堂课一堂课的模式,它没有什么优点。根据使用情况切换到一个测试类。从一个角度来看,您可能有多种情况:上限、下限等。为同一类中的那些创建不同的 @Test。
  2. 请记住,JUnit 将为每个 @Test 创建一个测试类的实例,因此每个测试将获得一个不同的固定装置(由相同的 @Before 方法设置)。如果您需要不同的固定装置,则需要不同的测试类,因为您处于不同的视角(参见 1)。
  3. 针对特定测试调整固定装置没有任何问题,但您应该尝试保持测试干净,以便它告诉您一个故事。当测试失败时,这个故事应该特别清晰,因此每个案例都有不同的、名称良好的 @Test(参见 1)。

Tips:

  1. Forget the one test class per class pattern, it has little merit. Switch to one test class per usage perspective. In one perspective you might have multiple cases: upper boundary, lower boundary, etc. Create different @Tests for those in the same class.
  2. Remember that JUnit will create an instance of the test class for each @Test, so each test will get a distinct fixture (set up by the same @Before methods). If you need a dissimilar fixture you need a different test class, because you are in a different perspective (see 1.)
  3. There is nothing wrong with tweaking the fixture for a particular test, but you should try to keep the test clean so it tells a story. This story should be particularly clear when the test fails, hence the different, well named @Test for each case (see 1.)
回眸一遍 2024-09-07 00:11:31

我建议根据您需要的不同灯具创建一个单独的类。如果您有两个不同的灯具,则只需创建两个不同的类(给它们一个方便的名称)。但我会再考虑一下这个问题,特别是赛程的差异以及为什么不同。也许您正在进行某种集成测试而不是单元测试?

I would suggest to create a separate Class based on the different fixtures you need. If you have two different fixtures you need just create two different classes (give them a convenient name). But i would think a second time about that, in particular about the difference in the fixtures and why are the different. May be you are on the way to a kind of integration test instead of unit test?

五里雾 2024-09-07 00:11:31

如果您确信您的装置对于单个测试是唯一的,那么它属于 @Test 方法。但这并不典型。它的某些部分可能是唯一的,或者您没有正确参数化/提取它,但通常您会在测试之间共享许多相同的数据。

最终夹具是测试的一部分。将固定装置放在 @Before 中被采用作为 xUnit 模式,因为测试总是:

  1. 准备测试数据/模拟
  2. 使用 SUT 执行操作
  3. 验证/断言状态/行为
  4. 销毁测试数据/模拟

和步骤 1 (@Before )和4(@After)在相关测试中被大量(至少部分)重用。由于 xUnit 非常重视测试独立性,因此它提供了固定方法来保证它们始终正确运行和测试创建/销毁的数据。

If you are positive that your fixture is unique to single test then it belongs to @Test method. This is not typical though. It could be that some part of it is unique or you didn't parametrize/extracted it right, but typically you will share a lot of the same data between tests.

Ultimately fixture is part of the test. Placing fixture in @Before was adopted as xUnit pattern because tests always:

  1. prepare test data/mocks
  2. perform operations with SUT
  3. validate/assert state/behavior
  4. destroy test data/mocks

and steps 1 (@Before) and 4 (@After) are reused a lot (at least partially) in related tests. Since xUnit is very serious about test independence it offers fixture methods to guarantee that they always run and test data created/destroyed properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文