Java 我希望基于excel中声明的运行模式运行测试用例

Java 我希望基于excel中声明的运行模式运行测试用例,java,junit,junit4,Java,Junit,Junit4,请查找以下代码和上次提到的查询 **注释列表** public interface AnnotationList{ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface QCID { String[] value(); } @Documented @Retention(RetentionPolicy.RUNTIME) @Target(

请查找以下代码和上次提到的查询

**注释列表**

public interface AnnotationList{

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface QCID {


   String[] value();

}

@Documented

    @Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface Author {


    String value();

}

@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 {


// TODO work in test status to see if things should be run.


public static final Logger logger = LoggerFactory.getLogger(JunitLink.class);


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

    super(klass);
  }


@Override

public void run(final RunNotifier notifier) {

    JunitLink.super.run(notifier);
}



    @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);

}

    logger.info("Test {} run completed", method.getName());
}


private int runNotIgnored(FrameworkMethod method,EachTestNotifier eachNotifier) {

    int failures = 0;

    eachNotifier.fireTestStarted();

try {

        methodBlock(method).evaluate();

} 
catch (AssumptionViolatedException e) {

        eachNotifier.addFailedAssumption(e);

        logger.error("Test {} failed!", method.getName());

        failures++;
    } 
catch (Throwable e) {

        eachNotifier.addFailure(e);

        logger.error("Test {} failed!", method.getName());

        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);

}}
//测试用例是这样编写的

@Author("XXXX")


@QCID({ "Smoke_TC01", "Smoke_TC02", "Smoke_TC03",
    "TC04"})

public class SmokeTest extends Startup{

private Components component = new Components();
private String TestDataSheetName = "Smoke";

public SmokeTest() throws Exception {

}

@MyTest
@Order(1)
public void openHomepage() throws Exception {
    component.openAPP();
}

@MyTest
@Order(2)
public void Login() throws Exception {
    component.Login(USID, PWD);

}

@MyTest
@Order(3)
public void isTerminalLocked() throws Exception {
    component.isTerminalLocked();
}
所有测试方法都按顺序运行。现在我只想运行特定的测试用例,这些测试用例的运行模式在excel中声明为“是”。我可以在每个测试用例之前添加extraline,以便从excel中读取行并运行特定的测试用例,但我希望读取excel并将所选测试用例(基于运行模式)传递给Junit runner


请帮帮我。

我认为你的问题“太宽泛”了。因为这个原因,它可能会关闭


看一看。因此,您的
issatified()
必须解析您的Excel。您可以使用类似的方法来实现这一点。

我在测试套件包中有许多测试用例,如SmokeTest.class文件(如上示例所示)。因此,您希望根据Excel电子表格中的数据有条件地禁用测试?感谢您的回复。因此,对于每个类,我都运行Issatified方法来检查运行模式。实际上,我正在使用jxl来完成这项工作。但是我想一次获得所有可运行的测试用例列表并调用它们,而不是每次都检查。我有900多个TC,每个TC(场景)我有几个方法(步骤)。将Excel读入数组或其他东西一次,然后每个TC只检查这个数组。请注意:这绝对是现在讨论的话题,而不是问答式,问答式是SO的首选形式。看见
@Author("XXXX")


@QCID({ "Smoke_TC01", "Smoke_TC02", "Smoke_TC03",
    "TC04"})

public class SmokeTest extends Startup{

private Components component = new Components();
private String TestDataSheetName = "Smoke";

public SmokeTest() throws Exception {

}

@MyTest
@Order(1)
public void openHomepage() throws Exception {
    component.openAPP();
}

@MyTest
@Order(2)
public void Login() throws Exception {
    component.Login(USID, PWD);

}

@MyTest
@Order(3)
public void isTerminalLocked() throws Exception {
    component.isTerminalLocked();
}