简体   繁体   English

Junit 4测试套件和个人测试类

[英]Junit 4 test suite and individual test classes

I have a JUnit 4 test suite with BeforeClass and AfterClass methods that make a setup/teardown for the following test classes. 我有一个带有BeforeClass和AfterClass方法的JUnit 4测试套件,可以对以下测试类进行设置/拆卸。 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. 我需要的是也是由他们自己运行测试类,但为此我需要为每个测试类设置/拆解场景(BeforeClass和AfterClass或类似的东西)。 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). 问题是,当我运行套件时,我不想在每个测试类之前和之后执行setup / teardown,我只想执行测试套件中的setup / teardown(一次)。 Is it possible ? 可能吗 ? Thanks in advance. 提前致谢。

I don't know of any standard way to do this with JUnit. 我不知道使用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. 但是,如果您确实确信自己在做什么,则可以使用全局标志来表示是否要运行类setup / teardown,并在类setup / teardown方法中检查其状态。 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. 需要注意的是,我担心JUnit不能保证套件中测试类的执行顺序,尽管很可能它确实以指定的顺序执行它们 - 但这只是一个实现细节。 Try it out, it may work for you - but there is no guarantee it will always do what you expect. 尝试一下,它可能适合你 - 但不能保证它会一直做你期望的。

If you have jUnit 4.7+ I recommend looking into the new feature called Rules (which are explained in this blog post ). 如果您有jUnit 4.7+,我建议您查看名为Rules的新功能( 本博文中对此进行了解释)。 They might not be exactly what you want, but they are probably the best you get with jUnit. 它们可能不是您想要的,但它们可能是您使用jUnit获得的最佳效果。

Supposedly TestNG has better test grouping possibilities, but I haven't really looked into it myself yet. 据说TestNG有更好的测试分组可能性,但我还没有真正研究过它。

No, there's no standard way to do this in JUnit, though you could hack something up as Péter Török suggested. 不,在JUnit中没有标准的方法可以做到这一点,尽管你可以像PéterTörök建议的那样解决问题。

Note however that you are more or less abusing JUnit in doing this. 但是请注意,在执行此操作时,您或多或少会滥用JUnit。 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... 所以我建议你强烈考虑是否总是运行设置并不是更好......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM