【问题标题】:Is it possible to run (via maven) select junit selenium tests based on a custom annotation是否可以基于自定义注释运行(通过 maven)选择 junit selenium 测试
【发布时间】:2016-12-01 19:12:36
【问题描述】:

试图获取问题中所有好的关键字。基本上我有一些硒测试,使用 JUnit4/Maven 并创建了一个自定义注释来用一些基本信息标记每个测试:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestInfo {
public enum Priority { LOW, MEDIUM, HIGH }

Priority priority() default Priority.LOW;
String useCase() default "";
String createdBy() default "unknown";
String lastModified() default "unknown";
}

所以每个测试看起来像这样:

@Test
@TestInfo(priority = TestInfo.Priority.HIGH,
        createdBy = "MivaScott",
        lastModified = "2016/11/29",
        useCase = "Log into website with valid credentials")
public void loginValidCredentials() throws Exception {
    Properties user = Data.getUserCredentials("default");
    loginPage.setLogin(user.getProperty("username"));
    loginPage.setPassword(user.getProperty("password"));
    loginPage.clickSignInButtonAndWait();
    Verify.titleContains(MyConstants.TITLE_DASHBOARD, "");
}

我希望我可以在命令上指定只运行标记为高优先级的测试。大意是这样的:

mvn -DTestInfo.priority=HIGH test

这是可能的,还是类似的?

【问题讨论】:

  • 你在用junit4吗?如果是这样,您应该查看类别。根据开箱即用的注释解决您对运行测试的要求。 github.com/junit-team/junit4/wiki/categories
  • @Grasshopper,我在寻找答案时看到了类别(尚未研究它们的工作原理)。我真的希望不必添加另一个注释/要记住的东西。我正在为非编码人员开发这个框架,所以移动部件越少越好。但是,是的,这是后备选项。

标签: java maven selenium junit


【解决方案1】:

我想有两种可能的方法来解决这个问题。

创建一个自定义测试运行器,它解析您的系统属性并仅运行带有匹配注解的测试方法。

public class PrioritzedTestRunner extends BlockJUnit4ClassRunner {


@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
    String priority = System.getProperty("TestInfo.priority");
    TestInfo info = method.getAnnotation(TestInfo.class);

    if (priority == null || info == null) {
        //If the configuration is blank or the test is uncategorized then run it
        super.runChild(method, notifier);
    } else if (priority != null) {
       //try to resolve the priority and check for a match.
        TestInfo.Priority configPri = TestInfo.Priority.valueOf(priority.toUpperCase());
        if (info.equals(configPri)) {
            super.runChild(method, notifier);
        }
    }
    }

您需要将 RunWith 注释添加到您的测试类中。

@RunWith(PrioritizedTestRunner.class)
 public voidMyTestClass() {
  @Test
  @TestInfo(...)     
  public void testThing1(){} 
  @Test
  @TestInfo(...)     
  public void testThing2(){}
}

如果测试是相当静态的,请将它们分成类而不是注释,并使用自定义 maven 配置文件来执行基于常用文件或资源的测试集合。

我自己没有配置这个,但我已经看到它完成了。您应该能够针对您的 PriorityLevels 进行 Maven 测试阶段。
http://maven.apache.org/guides/introduction/introduction-to-profiles.html

如果我正确阅读了文档,此时您应该能够将每个优先级作为单独的 mvn 命令执行。

祝你好运

【讨论】:

    【解决方案2】:

    我相信这个功能可以通过在你的 junit 测试用例中使用标签来实现。

    @WithTag(type="epic", name="Audit")

    参考:http://thucydides.info/docs/thucydides/_adding_tags_to_test_cases.html

    【讨论】:

    • @WithTag 注释是 Thucydides 库添加的注释,不是默认 JUnit 4 API 的一部分。使用此功能需要额外的测试范围库。
    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2015-06-10
    • 2012-09-05
    • 2013-08-23
    相关资源
    最近更新 更多