简体   繁体   English

为什么JUnit套件类不执行自己的Test,Before和After注释?

[英]Why does a JUnit suite class not execute its own Test, Before, and After annotations?

Why does a JUnit Suite class, in my cases its called TestSuite.class, not execute its own Test, Before, and After annotations? 为什么JUnit Suite类(在我的情况下称为TestSuite.class)不执行自己的Test,Before和After注释? It only exectutes its own BeforeClass, AfterClass, and then ALL annotations of the suite test classes. 它只会先执行自己的BeforeClass,AfterClass,然后再执行套件测试类的所有注释。 I proved this is the case by creating a test project around this theory: https://gist.github.com/djangofan/5033350 通过围绕该理论创建一个测试项目,我证明了这种情况: https : //gist.github.com/djangofan/5033350

Can anyone refer me to where this is explained? 谁能告诉我这解释的地方? I need to really understand this. 我需要真正理解这一点。

Because a TestSuite is not a Test itself. 因为TestSuite本身不是测试。 Those annotation are for unit tests only. 这些注释仅用于单元测试。 See here for an example. 请参阅此处的示例。

public class FeatureTestSuite {
  // the class remains empty <----- important for your question
}

A TestSuite is way of identifying a group of tests you wish apply some common behaviour to. TestSuite是识别希望应用某些常见行为的一组测试的方法。

Perhaps better explained with an example. 也许用一个例子更好地解释。

So say you were doing some basic CRUD tests on an Orders Table in a Database MyDB. 假设您正在对数据库MyDB中的订单表进行一些基本的CRUD测试。 Everyone needs mydb to be there and the orders table to exist, so you put them in a suite. 每个人都需要mydb并存在orders表,因此您将它们放在套件中。 It sets up the db and the table, the tests run, then before the suite goes out of scope the db is dropped, everything is nice and clean for the next test run. 它设置了数据库和表,然后运行测试,然后在套件超出范围之前删除数据库,对于下一次测试运行,一切都很好而且干净。 Otherwise you'd have to do that in every test which is expensive, or worse have test data from previous tests cause the others to fail often apparently randomly as you would have created an implicit dependancy between them. 否则,您将必须在每次测试中都这样做,因为这很昂贵,或者更糟的是,先前测试中的测试数据会导致其他测试经常明显地随机失败,因为您会在它们之间创建隐式依赖关系。 There are other ways of achieving the same thing, but they clutter up your tests and you have to remember to call them. 还有其他方法可以实现同一目标,但是它们会使您的测试混乱不堪,您必须记住将其称为“测试”。

You don't have to test it. 您不必测试。 If it doesn't get done none of your tests will execute. 如果未完成,将不会执行任何测试。

As others have said, it's because a TestSuite is not a Test . 正如其他人所说,这是因为TestSuite不是Test It's just a class with an annotation to group other tests, so that it is more convenient to run. 它只是一个带有注释的类,可以对其他测试进行分组,因此运行起来更加方便。

It does have one special property, however, and that is the execution of @BeforeClass and @AfterClass . 但是,它确实具有一个特殊属性,即@BeforeClass@AfterClass的执行。 These are enabled to allow a global setup/teardown for the suite. 启用这些功能可以对套件进行全局设置/拆卸。 It does not execute any tests (including @After , @Before or any rules). 它不执行任何测试(包括@After@Before或任何规则)。

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

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