[英]How to run test methods in order with Junit
I am using JUnit and Selenium Webdriver. 我正在使用JUnit和Selenium Webdriver。 I want to run my test methods in order as how I write them in my code, as below: 我想按照我在代码中编写测试方法的顺序运行我的测试方法,如下所示:
@Test
public void registerUserTest(){
// code
}
@Test
public void welcomeNewUserTest(){
// code
}
@Test
public void questionaireNewUserTest(){
// code
}
But it doesn't work, it always executes my test methods in this order: 但它不起作用,它总是按此顺序执行我的测试方法:
welcomeNewUserTest()
registerUserTest()
questionaireNewUserTest()
I read an answer somewhere if I name my method with suffix Test, then JUnit would execute them in order as how I order them in code. 如果我用后缀Test命名我的方法,那么我在某处读了一个答案,然后JUnit将按顺序执行它们,就像我在代码中命令它们一样。 Apparently, this doesn't work. 显然,这不起作用。
Any help? 有帮助吗? Thanks 谢谢
So for tests like these - where the steps are dependent on each other - you should really execute them as one unit. 因此,对于像这样的测试 - 步骤彼此依赖 - 您应该将它们作为一个单元执行。 You should really be doing something like: 你真的应该这样做:
@Test
public void registerWelcomeAndQuestionnaireUserTest(){
// code
// Register
// Welcome
// Questionnaire
}
As @Jeremiah mentions below, there are a handful of unique ways that separate tests can execute unpredictably. 正如@Jeremiah在下面提到的,有一些独特的方法可以让不可预测的单独测试执行。
Now that I've said that, here's your solution. 现在我已经说过了,这是你的解决方案。
If you want separate tests, you can use @FixMethodOrder and then do it by NAME_ASCENDING
. 如果您需要单独的测试,可以使用@FixMethodOrder ,然后通过NAME_ASCENDING
。 This is the only way I know. 这是我所知道的唯一方式。
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestMethodOrder {
@Test
public void testA() {
System.out.println("first");
}
@Test
public void testC() {
System.out.println("third");
}
@Test
public void testB() {
System.out.println("second");
}
}
will execute: 将执行:
testA(), testB(), testC()
In your case: 在你的情况下:
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ThisTestsEverything{
@Test
public void T1_registerUser(){
// code
}
@Test
public void T2_welcomeNewUser(){
// code
}
@Test
public void T3_questionaireNewUser(){
// code
}
}
You can not run your test methods in order as how they are written. 您无法按照编写方式运行测试方法。 The point is test must be independent each other. 关键是测试必须相互独立。 JUnit doesn't encourage dependent tests. JUnit不鼓励依赖测试。
But if you are very want... 但如果你非常想......
There is the @FixMethodOrder annotation. 有@FixMethodOrder注释。 Please, read the following Annotation Type FixMethodOrder 请阅读以下注释类型FixMethodOrder
You can sort methods with @FixMethodOrder(MethodSorters.NAME_ASCENDING)
annotation. 您可以使用@FixMethodOrder(MethodSorters.NAME_ASCENDING)
注释对方法进行排序。 Like, 喜欢,
@FixMethodOrder(MethodSorters.DEFAULT)
public class DefaultOrderOfExecutionTest { private static StringBuilder output = new StringBuilder(""); public class DefaultOrderOfExecutionTest {private static StringBuilder output = new StringBuilder(“”);
@Test
public void secondTest() {
output.append("b");
}
@Test
public void thirdTest() {
output.append("c");
}
@Test
public void firstTest() {
output.append("a");
}
@AfterClass
public static void assertOutput() {
assertEquals(output.toString(), "cab");
}
} }
You can perform sorting in 3 ways: 您可以通过3种方式执行排序:
For more details please refer: The Order of Tests in JUnit 有关更多详细信息,请参阅: JUnit中的测试顺序
Use the following command above the class from which you will execute your tests 在要执行测试的类上方使用以下命令
@FixMethodOrder(MethodSorters.JVM)
public class TestMethodOrder {
@Test
public void signup() {
System.out.println("Signup");
}
@Test
public void login() {
System.out.println("Login");
}
@Test
public void navigate() {
System.out.println("Navigate");
}
}
The MethodSorters.JVM annotation will execute your tests in the way that you have actually written in your file. MethodSorters.JVM注释将以您实际写入文件的方式执行测试。 (Just as the same way that Java code executes, line by line) (就像Java代码一样逐行执行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.