問(wèn)題描述
我正在使用 JUnit 和 Selenium Webdriver.我想按照我在代碼中編寫(xiě)它們的順序運(yùn)行我的測(cè)試方法,如下所示:
I am using JUnit and 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
}
但它不起作用,它總是按這個(gè)順序執(zhí)行我的測(cè)試方法:
But it doesn't work, it always executes my test methods in this order:
welcomeNewUserTest()
registerUserTest()
questionaireNewUserTest()
如果我用后綴 Test 命名我的方法,我會(huì)在某處讀到答案,然后 JUnit 會(huì)按照我在代碼中對(duì)它們的排序方式執(zhí)行它們.顯然,這行不通.
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. Apparently, this doesn't work.
有什么幫助嗎?謝謝
推薦答案
所以對(duì)于像這樣的測(cè)試——步驟相互依賴——你應(yīng)該真正將它們作為一個(gè)單元來(lái)執(zhí)行.你真的應(yīng)該做這樣的事情:
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
}
正如@Jeremiah 下面提到的,有一些獨(dú)特的方法可以使單獨(dú)的測(cè)試無(wú)法預(yù)測(cè)地執(zhí)行.
As @Jeremiah mentions below, there are a handful of unique ways that separate tests can execute unpredictably.
既然我已經(jīng)說(shuō)過(guò)了,這就是你的解決方案.
Now that I've said that, here's your solution.
如果你想要單獨(dú)的測(cè)試,你可以使用 @FixMethodOrder 然后按 NAME_ASCENDING
執(zhí)行.這是我知道的唯一方法.
If you want separate tests, you can use @FixMethodOrder and then do it by 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");
}
}
將執(zhí)行:
testA(), testB(), testC()
在你的情況下:
@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
}
}
這篇關(guān)于如何使用 Junit 按順序運(yùn)行測(cè)試方法的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!