Junit 4 测试套件和单独的测试类

发布于 2024-08-23 05:00:19 字数 212 浏览 7 评论 0原文

我有一个 JUnit 4 测试套件,其中包含 BeforeClass 和 AfterClass 方法,可以为以下测试类进行设置/拆卸。 我需要的是自己运行测试类,但为此我需要为每个测试类提供一个设置/拆卸场景(BeforeClass 和 AfterClass 或类似的东西)。问题是,当我运行套件时,我不想在每个测试类之前和之后执行安装/拆卸,我只想从测试套件执行安装/拆卸(一次)。 是否可以 ?提前致谢。

I have a JUnit 4 test suite with BeforeClass and AfterClass methods that make a setup/teardown for the following test classes.
What I need is to run the test classes also by them selves, but for that I need a setup/teardown scenario (BeforeClass and AfterClass or something like that) for each test class. The thing is that when I run the suite I do not want to execute the setup/teardown before and after each test class, I only want to execute the setup/teardown from the test suite (once).
Is it possible ? Thanks in advance.

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

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

发布评论

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

评论(3

靖瑶 2024-08-30 05:00:19

我不知道使用 JUnit 执行此操作有任何标准方法。您可能已经知道,其原因是您的测试用例应该彼此独立运行。这涉及在每个测试方法之前和之后运行的“正常”设置/拆卸方法。不过,类的设置和拆卸有点不同 - 尽管我仍然更喜欢独立运行我的测试并远离麻烦区。

但是,如果您确实确信自己在做什么,则可以使用全局标志来指示类设置/拆卸是否要运行,并在类设置/拆卸方法中检查其状态。在您的测试套件中,您可以包含一个特殊的类作为第一个类,它只执行设置并设置全局标志以向真实的测试用例指示不得运行其类设置/拆卸方法。同样,套件中的最后一个特殊类可以执行拆卸代码。需要注意的是,我担心 JUnit 不能保证套件内测试类的执行顺序,尽管它很可能确实按指定的顺序执行它们 - 但这只是一个实现细节。尝试一下,它可能对您有用 - 但不能保证它总是能达到您的期望。

I don't know of any standard way to do this with JUnit. The reason for it, as you probably already know, is that your test cases should run independently of each other. This concerns the "normal" setup/teardown methods which run before and after each test method. Class setup and teardown is a bit different though - although I would still prefer running my tests independently and staying out of the trouble zone.

However, if you really are convinced of what you are doing, you could use a global flag to signal whether or not the class setup/teardown is to run, and to check for its state in the class setup/teardown methods. In your test suite, you could include a special class as the very first one, which does nothing more than execute the setup and set the global flag to indicate to the real test cases that their class setup/teardown methods must not be run. Similarly, a special last class in the suite can execute the teardown code. The caveat is that I am afraid JUnit does not guarantee the order of execution of test classes inside a suite, although most probably it does execute them in the specified order - but this is just an implementation detail. Try it out, it may work for you - but there is no guarantee it will always do what you expect.

迷荒 2024-08-30 05:00:19

如果您有 jUnit 4.7+,我建议您研究名为 Rules 的新功能(在 这篇博文)。它们可能不是您想要的,但它们可能是您使用 jUnit 得到的最好的。

据说 TestNG 具有更好的测试分组可能性,但我自己还没有真正研究过。

If you have jUnit 4.7+ I recommend looking into the new feature called Rules (which are explained in this blog post). They might not be exactly what you want, but they are probably the best you get with jUnit.

Supposedly TestNG has better test grouping possibilities, but I haven't really looked into it myself yet.

陌若浮生 2024-08-30 05:00:19

不,JUnit 中没有标准方法可以做到这一点,尽管您可以按照 Péter Török 的建议进行一些修改。

但请注意,您在执行此操作时或多或少滥用了 JUnit。单元测试的重点是它们彼此独立。这是因为测试之间的依赖关系造成了维护的噩梦(测试因以错误的顺序运行而失败)。

因此,我建议您强烈考虑始终运行设置是否不是更好......

No, there's no standard way to do this in JUnit, though you could hack something up as Péter Török suggested.

Note however that you are more or less abusing JUnit in doing this. The whole point of unit tests it that they are independent of each other. This is because dependencies between tests create a total maintenance nightmare (tests failing because the run in the wrong order).

So I'd advise you to strongly consider if it's not better to just always run the setup...

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