java JUnit4 是否开始支持测试排序?是故意的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7845929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Has JUnit4 begun supporting ordering of test? Is it intentional?
提问by Tarun
A newbie to JUnit (in fact JUnit 4) and came across suite way of executing test
JUnit 的新手(实际上是 JUnit 4)并且遇到了执行测试的套件方式
@RunWith(Suite.class)
@Suite.SuiteClasses(
{
CreateNewProfile.class,
EditProfile.class,
})
public class ProfileTestSuite {
}
This is the code sample I came across while browsing through the test code base at my new employer. During execution I fund that - CreateNewProfile tests are executed first and then EditProfile, which does make sense but then it introduces dependency among tests.
这是我在新雇主浏览测试代码库时遇到的代码示例。在执行期间,我资助 - 首先执行 CreateNewProfile 测试,然后执行 EditProfile,这确实有意义,但随后会在测试之间引入依赖性。
I have been following non dependent test mechanism from couple of months (though I used to use TestNG and not JUnit) and would expect EditProfile being able to be executed in isolation as well. That is edit profile should take care of creating profile and then editing it and then asserting the operations.
几个月以来,我一直在遵循非依赖测试机制(尽管我曾经使用 TestNG 而不是 JUnit),并且希望 EditProfile 也能够单独执行。那就是编辑配置文件应该负责创建配置文件,然后编辑它,然后断言操作。
My question here is - has Junit 4 introduced test ordering feature. Is this feature intended or one easter egg as I always felt JUnit = independent tests.
我的问题是 - Junit 4 是否引入了测试排序功能。这个特性是有意的还是一个复活节彩蛋,因为我一直觉得 JUnit = 独立测试。
回答by Matthew Farwell
No JUnit does not support test ordering, except in the manner that you say, through Suite
.
This only defines the order in which the test classesare executed. This has been there for a long time, including JUnit 3 Suite classes.
没有 JUnit 不支持测试排序,除非你说的方式,通过Suite
. 这仅定义了执行测试类的顺序。这已经存在很长时间了,包括 JUnit 3 Suite 类。
For a more complete explanation, there are three things we need to talk about here:
为了更完整的解释,这里我们需要讲三件事:
- the ordering of tests classes within a test suite
- the ordering of test classes when they are found by reflection by Eclipse or Maven
- the ordering of test methods (annotated with @Test) within a test class.
- 测试套件中测试类的排序
- 通过 Eclipse 或 Maven 反射找到测试类时的排序
- 测试类中测试方法的顺序(用@Test 注释)。
The ordering of tests classes within a test suite
测试套件中测试类的排序
When you specify the list of classes to be executed in a test suite, you're defining an array, and these test classes will be executed in order, except when you're doing parallel execution. Unfortunately, this allows the introduction of dependencies between your test classes.
当您指定要在测试套件中执行的类列表时,您就是在定义一个数组,这些测试类将按顺序执行,除非您进行并行执行。不幸的是,这允许在您的测试类之间引入依赖项。
The ordering of test classes when they are found by reflection
通过反射找到测试类时的排序
When searching the classpath for classes, the order in which they are found is not guaranteed, so cannot be depended upon. It isn't actually JUnit which is doing the searching, but the Eclipse Junit plugin, or maven surefire or failsafe.
在类路径中搜索类时,不能保证找到它们的顺序,因此不能依赖。进行搜索的实际上并不是 JUnit,而是 Eclipse Junit 插件,或者 maven surefire 或 failsafe。
The ordering of test methods within a test class
测试类中测试方法的排序
JUnit does not guarantee the order of execution of tests within a class. Most of the time, on most JVMs pre version 7, the order in which they are found using reflection is in declaration order, ie the order they are in the file. This is the order in which they are executed. However, with JVM 7, this is no longer guaranteed, so there won't be a consistent order. There is a github issue #293 Sort test methods for predictabilityopen with suggested solutions, and there is a thread on the junit mailing list: Alphabetizing test method run order?. So you cannot depend upon the order that tests will be executed using JUnit, but this is currently under discussion.
JUnit 不保证类中测试的执行顺序。大多数情况下,在版本 7 之前的大多数 JVM 上,使用反射找到它们的顺序是声明顺序,即它们在文件中的顺序。这是它们执行的顺序。但是,对于 JVM 7,这不再有保证,因此不会有一致的顺序。有一个 github 问题#293 Sort test methods for predictabilityopen withSuggestedsolutions,junit 邮件列表上有一个线程:按字母顺序排列测试方法运行顺序?. 因此,您不能依赖于使用 JUnit 执行测试的顺序,但这目前正在讨论中。
回答by jeha
No, ordering is not supported. And yes, it is intention:
不,不支持订购。是的,这是意图:
KentBeck commented (December 30, 2010):Independent tests are more valuable, so JUnit by design does not support test ordering.
KentBeck 评论(2010 年 12 月 30 日):独立测试更有价值,因此 JUnit 按设计不支持测试排序。
If you need this feature, you could use TestNG with it's @Test(sequential = true)
.
如果您需要此功能,您可以将 TestNG 与它的@Test(sequential = true)
.
回答by Tomasz Nurkiewicz
@jehaanswer is very good but he talks about test methods (the ones annotated with @Test
) order. This order is indeed unspecified, probably because the order of methods in a class obtained via reflection is not guaranteed to correspond with the order of methods in the source file.
@jeha 的回答非常好,但他谈到了测试方法(用 注释的方法@Test
)顺序。这个顺序确实是未指定的,可能是因为通过反射获得的类中方法的顺序并不能保证与源文件中方法的顺序相对应。
However from my experience they are executed in the same order they were defined most of the time.
然而,根据我的经验,它们的执行顺序与大多数时候定义的顺序相同。
You are asking about the order of test classes. This should not be a surprise: you are explicitly listing test classes using @Suite.SuiteClasses
and JUnit probably has no business in shuffling them and running in different order. Are you concerned about introducing dependency between tests simply because you explicitly ordered them?
您正在询问测试类的顺序。这应该不足为奇:您明确地列出了使用的测试类,@Suite.SuiteClasses
而 JUnit 可能没有办法改变它们并以不同的顺序运行。您是否担心仅仅因为您明确订购它们而在测试之间引入依赖关系?
回答by Masá?
You can extend the Suite runner:
您可以扩展套件运行器:
public class MySuite extends Suite {
public SuiteRunner(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
}
@Override
protected List<Runner> getChildren() {
List<Runner> children = super.getChildren();
... here modify the children list - you can remove or reorder test...
return children;
}
}
Then annotate your suite:
然后注释您的套件:
@RunWith(MySuite.class)
@Suite.SuiteClasses({
CreateNewProfile.class,
EditProfile.class})
public class ProfileTestSuite { }
The tests marked with @Ignore
will be represented by IgnoredClassRunner
and regular test will be represented by BlockJUnit4ClassRunner
(or whatever runner you will annotate them - which will be probably extension of BlockJUnit4ClassRunner
).
标记为 的测试@Ignore
将由 表示IgnoredClassRunner
,常规测试将由BlockJUnit4ClassRunner
(或您将对其进行注释的任何运行程序 - 这可能是 的扩展BlockJUnit4ClassRunner
)表示。
From the runner you can get the test class. Then you can use own annotations.
从跑步者你可以得到测试类。然后你可以使用自己的注释。
回答by Aleksandr Dubinsky
JUnit 4.11 now supports specifying execution order using @FixMethodOrder annoation.
JUnit 4.11 现在支持使用 @FixMethodOrder 注释指定执行顺序。
回答by Kelly Ryan
Just bypass it by ordering the names of the class by using an _# before the class name. Ex _1testcaselogin _2testcaseverify
只需通过在类名前使用 _# 对类名进行排序来绕过它。例如 _1testcaselogin _2testcaseverify