按参数选择Selenium测试

问题描述 投票:0回答:1

我们使用Selenium Webdriver构建了一些测试。使用JUnit注释我们手动选择运行哪些测试(@Test,@ Ignore)。

像这样的东西:

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; 
import ...

@RunWith(JUnit4.class)
public class MainclassTest {

  @Test
  @Ignore
  public void Test01() {
    ...
  }

  @Test
  // @Ignore
  public void Test02() {
    ...
  }

}

以上我们只想运行Test02。

但是现在我们想通过Jenkins运行这个测试并通过参数选择一个或多个测试而不是注释掉@Ignore。在Jenkins中,我们只使用-Dxxxx提供POM文件和一些参数。

在不同的jenkins工作中运行不同测试组合的好方法是什么?将测试分成不同的类更好吗?或者我可以在maven pom文件中更好地配置想要的测试吗?

maven selenium jenkins junit parameters
1个回答
2
投票

你可以使用JUnit categories

作为优先级的一个简单示例,声明接口并扩展层次结构,如下所示:

/** Marker for test case priority. * /
package priority;
public interface Low {
}
public interface Medium extends Low {
}
public interface High extends Medium {
}

然后根据需要注释您的方法,例如:

public class MainclassTest {
  @Test
  @Category(priority.High.class)
  public void Test01() {
    ...
  }
  @Test
  @Category(priority.Low.class)
  public void Test02() {
    ...
  }
}

最后让您的POM可配置

<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <groups>${testcase.priority}</groups>
      </configuration>
    </plugin>
  </plugins>
</build>

让Jenkins根据需要使用参数运行它:

mvn test -Dtestcase.priority=priority.High

(注意,由于接口的扩展,Low将运行所有类别。如果您不想这样,只需删除扩展名)。

© www.soinside.com 2019 - 2024. All rights reserved.