[英]How to only execute the first test in a Test class with TestNG
I'm putting together a navigation test suite to exercise a little bit of functionality on each page of my company's internal application with Java / Selenium / TestNG.我正在组装一个导航测试套件,以在我公司内部应用程序的每个页面上使用 Java / Selenium / TestNG 来练习一些功能。
Say I have a test class called ApplicationsIT in my NavigationTests directory.假设我在我的 NavigationTests 目录中有一个名为 ApplicationsIT 的测试 class。 Within this class, I have 10 @Tests annotations so every time I run the full NavigationTests suite all 10 Tests run.
在这个 class 中,我有 10 个 @Tests 注释,所以每次我运行完整的 NavigationTests 套件时,所有 10 个测试都会运行。
But, for the sake of time, is there a way for me to only run the first test in each test class?但是,为了时间,有没有办法让我在每个测试 class 中只运行第一个测试?
I tried messing with the "(alwaysRun = false)" tags but it didn't seem to work how I'd expected.我试着弄乱了“(alwaysRun = false)”标签,但它似乎并没有像我预期的那样工作。
I know this is a poorly constructed question/post so I'd be happy to clarify anything I can.我知道这是一个结构不佳的问题/帖子,所以我很乐意尽我所能澄清一切。
There are at-least two ways of doing this dynamically.至少有两种动态执行此操作的方法。
org.testng.IAnnotationTransformer
.org.testng.IAnnotationTransformer
的实现。 Here you basically implement the interface org.testng.IAnnotationTransformer
and within its transform()
method, you inspect the incoming testMethod
java.lang.reflect.Method
object to check if its name matches with what you want to run or not run.org.testng.IAnnotationTransformer
并在其transform()
方法中检查传入的testMethod
java.lang.reflect.Method
object 以检查其名称是否与您要运行或不运行的名称匹配。 You can specify the intent to run/not run a bunch of methods via a JVM argument.annotation.setEnabled(false)
here annotation
is of type org.testng.annotations.ITestAnnotation
.annotation.setEnabled(false)
禁用测试方法,此处annotation
的类型为org.testng.annotations.ITestAnnotation
。 You would need to wire in this listener via a service loader (or) using the <listeners>
tag in your suite xml file.<listeners>
标记连接此侦听器。 For more information on listeners in general, you can take a look my blog post here<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="47230879_Suite" parallel="false" verbose="2">
<test name="47230879_test" verbose="2">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
try {
input = System.getProperty("methods","");
if (input.trim().isEmpty()) {
return true;
}
methods= input.split(",");
for (int i =0; i< methods.length; i++) {
if (method.getName().equals(methods[i])) {
return true;
}
}
return false;
} catch( e ) {
print(e);
}
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="com.rationaleemotions.stackoverflow.qn47230879.TestclassSample"/>
</classes>
</test>
</suite>
The test class looks like below测试 class 如下所示
package com.rationaleemotions.stackoverflow.qn47230879;
import org.testng.annotations.Test;
public class TestclassSample {
@Test
public void a() {
System.err.println("a()");
}
@Test
public void b() {
System.err.println("b()");
}
@Test
public void c() {
System.err.println("c()");
}
@Test
public void d() {
System.err.println("d()");
}
@Test
public void e() {
System.err.println("e()");
}
}
If you run the suite without specifying any methods via JVM arguments, all methods get run, but if you specify a bunch of method names via JVM argument -Dmethods=a,b,c
(for eg,) then only those methods get executed.如果您在未通过 JVM arguments 指定任何方法的情况下运行套件,所有方法都会运行,但如果您通过 JVM 参数
-Dmethods=a,b,c
(例如,)指定一堆方法名称,则只会执行这些方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.