如何运行junit+;硒测试有序

如何运行junit+;硒测试有序,junit,selenium-webdriver,selenium-rc,junit4,junit3,Junit,Selenium Webdriver,Selenium Rc,Junit4,Junit3,嗨,我是JUnit测试新手 我用selenium代码运行JUnit程序,它不是自上而下运行的,而是随机运行的 但是我想按顺序执行程序,包括登录、创建、更新、删除等功能 但是,它就像 我想按顺序运行这个程序。请将您宝贵的建议发送给我。您可以使用测试套件设置JUnit的类的顺序: import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({

嗨,我是JUnit测试新手

我用selenium代码运行JUnit程序,它不是自上而下运行的,而是随机运行的

但是我想按顺序执行程序,包括登录、创建、更新、删除等功能

但是,它就像


我想按顺序运行这个程序。请将您宝贵的建议发送给我。

您可以使用测试套件设置JUnit的类的顺序:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
   TestJunit1.class,
   TestJunit2.class
})
public class JunitTestSuite {   
}  
并使用@FixMethodOrder设置类中的测试顺序(从JUnit4.11开始)


注释列表

public interface AnnotationList{


@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface Order {


int value();

}


@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface MyTest {


static class None extends Throwable {

    private static final long serialVersionUID = 1L;


    private None() {


   }}
JUNITLink类

public class JunitLink extends BlockJUnit4ClassRunner {



public JunitLink(Class<?> klass) throws InitializationError {

    super(klass);
}
@Override

protected List<FrameworkMethod> computeTestMethods() {

List<FrameworkMethod> classMethods = getTestClass().getAnnotatedMethods(AnnotationList.MyTest.class);

SortedMap<Integer, FrameworkMethod> sortedTestMethodList = new TreeMap<Integer, FrameworkMethod>();

    for (FrameworkMethod seleniumTest : classMethods) {

        if (seleniumTest.getAnnotation(AnnotationList.Order.class) != null)          {   

        sortedTestMethodList.put(seleniumTest.getAnnotation(AnnotationList.Order.class).value(),seleniumTest);

        }

    }

    return new ArrayList<FrameworkMethod>(sortedTestMethodList.values());

}


@Override

protected void runChild(FrameworkMethod method, RunNotifier notifier) {

    EachTestNotifier eachNotifier = makeNotifier(method, notifier);

if (method.getAnnotation(Ignore.class) != null) {

        runIgnored(eachNotifier);

    } else {

        runNotIgnored(method, eachNotifier);

}


}


private int runNotIgnored(FrameworkMethod method,EachTestNotifier eachNotifier) {

    int failures = 0;

    eachNotifier.fireTestStarted();

try {

        methodBlock(method).evaluate();

} 
catch (AssumptionViolatedException e) {

        eachNotifier.addFailedAssumption(e);



        failures++;

} 
    catch (Throwable e) {

        eachNotifier.addFailure(e);


        failures++;

} finally {

        eachNotifier.fireTestFinished();

    }

    return failures;

}


    private void runIgnored(EachTestNotifier eachNotifier) {


    eachNotifier.fireTestIgnored();

}


    private EachTestNotifier makeNotifier(FrameworkMethod method,RunNotifier notifier) {

    Description description = describeChild(method);


return new EachTestNotifier(notifier, description);

}

}
TestCases这应该扩展上面创建的启动类

public class Testcase extends StartUp{



public SmokeTest() throws Exception {

}

@Test
@Order(1)
// Wrire test method
}

@Test
@Order(2)
// Test Case 2

}

@Test
@Order(3)
//Test case 3
}

您可以更改附加了“Test”的TestName。 例如:

@Test
public void loginTest(){
// Wrire test method
}

@Test
public void creationTest(){
// Test Case 2

}

@Test
public vid updateTest(){
//Test case 3
}

如果您使用loginTest()、creationTest()和updateTest()而不是login()、create()和update(),那么Junit将按照指定的顺序执行所有测试。

您也可以使用@FixMethodOrder(MethodSorters.JVM)


因为在这种情况下,它将按照方法实现的顺序执行。

请看这篇文章:感谢回复,但我尝试了两个答案,没有用。感谢回复,但我尝试了两个答案,没有用。订单仍然有问题。我在这里添加了我的示例执行代码,请查找并提供有价值的解决方案。测试套件跑步者![在此处输入图像描述][1]并正常运行junit![在此处输入图像描述][2]也许需要一点解释?
public class Testcase extends StartUp{



public SmokeTest() throws Exception {

}

@Test
@Order(1)
// Wrire test method
}

@Test
@Order(2)
// Test Case 2

}

@Test
@Order(3)
//Test case 3
}
@Test
public void loginTest(){
// Wrire test method
}

@Test
public void creationTest(){
// Test Case 2

}

@Test
public vid updateTest(){
//Test case 3
}