问题描述
我有两个逻辑类别的测试:纯粹的功能单元测试(通过/失败)和仅用于度量/诊断的基准性能测试。
I have two logical categories of tests: plain functional unit tests (pass/fail) and benchmark performance tests that are just for metrics/diagnostics.
目前,我在一个类中有所有测试方法,称之为 MyTests
:
Currently, I have all test methods in a single class, call it MyTests
:
public class MyTests
{
@Test
public void testUnit1()
{
...
assertTrue(someBool);
}
@Test
public void testUnit2()
{
...
assertFalse(someBool);
}
@Test
@Category(PerformanceTest.class)
public void bmrkPerfTest1()
{
...
}
@Test
@Category(PerformanceTest.class)
public void bmrkPerfTest2()
{
...
}
}
然后我有一个 UnitTestSuite
定义为
@RunWith(Categories.class)
@Categories.ExcludeCategory(PerformanceTest.class)
@SuiteClasses({ MyTests.class })
public class UnitTestSuite {}
和a PerformanceTestSuite
@RunWith(Categories.class)
@Categories.IncludeCategory(PerformanceTest.class)
@SuiteClasses({ MyTests.class })
public class PerformanceTestSuite {}
,以便我可以在性能测试中与 Ant
分开运行单元测试(我不认为包含Ant代码是必需的) )
so that I can run the unit tests in Ant
separately from performance tests (I don't think including Ant code is necessary).
这意味着我共有四个类(MyTests,PerformanceTest,PerformanceTestSuite和UnitTestSuite)。我意识到我可以将所有的单元测试放在一个类中,并在另一个类中进行基准测试,并且完成它,而不会增加类别和额外的注释的复杂性。我通过Ant中的类名调用测试,即不要在程序包中运行所有测试。
This means I have a total of FOUR classes (MyTests, PerformanceTest, PerformanceTestSuite, and UnitTestSuite). I realize I could have just put all the unit tests in one class and benchmark tests in another class and be done with it, without the additional complexity with categories and extra annotations. I call tests by class name in Ant, i.e. don't run all tests in a package.
是否有意义,按类别进行组织的原因是什么注释或者如果我只是在两个简单的测试类中重构它会更好吗?
Does it make sense and what are the reasons to keep it organized by category with the annotation or would it be better if I just refactored it in two simple test classes?
推荐答案
关于是否将测试分为两类:
To the question of whether to split the tests in two classes:
由于他们显然是非常不同种类的测试(单元测试和性能测试),我会把它们在任何情况下,在不同的班级,只因为这个原因。
As they are clearly very different kinds of tests (unit tests and performance tests), I would put them in different classes in any case, for that reason alone.
还有一些想法:
我不认为使用 @Category
注释是一个坏主意。我会做的,在一个更典型的项目中有几十个或几百个包含测试的类,它用 @Category $ c来注释测试类(而不是方法) $ c>,然后,以避免重复分类测试的努力。 (也许。)
I don't think using @Category
annotations is a bad idea however. What I'd do, in a more typical project with tens or hundreds of classes containing tests, is annotate the test classes (instead of methods) with @Category
, then use the ClassPathSuite library to avoid duplicated efforts of categorising the tests. (And maybe run the tests by category using Ant.)
如果你只会有两个测试类,那当然不重要。如果有额外的类错误,你可以保留Categories和套件,或者将它们丢弃(如你所说的测试由Ant中的类名称运行)。我会保留他们,并按照通常(在一个健康的项目)中进行上述情况,更多的测试将随着时间的推移积累。 : - )
If you will only ever have the two test classes, it of course doesn't matter much. You can keep the Categories and Suites, or throw them away (as you said the tests are run by class name in Ant) if having the extra classes bugs you. I'd keep them, and move towards the scenario described above, as usually (in a healthy project) more tests will accumulate over time. :-)
这篇关于使用JUnit类别,而不是简单地在单独的类中组织测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!