我们已经创建了硒测试用例,并使用TestNg框架来执行。
我已包含优先级并与@Test一起启用。
但是该团队希望他们希望拥有优先级,并希望从xls表中启用它以便于使用。
我还没有看到任何类似TestNg支持此博客的博客。
任何人都可以确认是否可以实施?
答案 0 :(得分:2)
是的。可以从excel电子表格控制优先级,并根据此优先级分配相应的优先级。为此,您需要一个org.testng.IAnnotationTransformer
实现,可以通过TestNG套件xml文件将其插入。
这是您可以执行的操作。
我正在使用:TestNG 6.14.3,并且为了读取csv(出于本示例的目的),我正在使用库
<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.3</version>
</dependency>
示例csv的外观如下
| class_name | method_name | priority |
|----------------------------------------------------------------|-------------|----------|
| com.rationaleemotions.stackoverflow.qn50998867.TestclassSample | first | 1 |
| com.rationaleemotions.stackoverflow.qn50998867.TestclassSample | second | 2 |
| com.rationaleemotions.stackoverflow.qn50998867.TestclassSample | third | 3 |
要应用优先级的示例测试类
package com.rationaleemotions.stackoverflow.qn50998867;
import org.testng.annotations.Test;
public class TestclassSample {
@Test
public void second() {
System.err.println("second");
}
@Test
public void first() {
System.err.println("first");
}
@Test
public void third() {
System.err.println("third");
}
}
TestNG侦听器(org.testng.IAnnotationTransformer
实现)的外观如下:
package com.rationaleemotions.stackoverflow.qn50998867;
import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class PriorityTransformer implements IAnnotationTransformer {
private boolean initialised = false;
private List<PriorityInformation> info = new ArrayList<>();
private void init() {
String file = System.getProperty("file.location", "src/test/resources/50998867.csv");
try {
CSVReader reader = new CSVReader(new FileReader(file));
List<String[]> allData = reader.readAll();
boolean firstRow = true;
for (String[] each : allData) {
if (firstRow) {
firstRow = false;
continue;
}
info.add(new PriorityInformation(each));
}
initialised = true;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void transform(
ITestAnnotation annotation, Class clazz, Constructor constructor, Method method) {
PriorityInformation dummy =
new PriorityInformation(method.getDeclaringClass().getName(), method.getName());
if (!initialised) {
init();
}
PriorityInformation found =
info.stream()
.filter(priorityInformation -> priorityInformation.equals(dummy))
.findFirst()
.orElse(PriorityInformation.EMPTY);
if (found.equals(PriorityInformation.EMPTY)) {
return;
}
if (info.contains(dummy)) {
annotation.setPriority(found.getPriority());
}
}
}
TestNG套件xml文件如下所示
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="StackOverflow_50998867_suite" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.stackoverflow.qn50998867.PriorityTransformer"/>
</listeners>
<test name="StackOverflow_50998867_test">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn50998867.TestclassSample"/>
</classes>
</test>
</suite>
输出内容如下:
...
... TestNG 6.14.3 by Cédric Beust (cedric@beust.com)
...
first
second
third
PASSED: first
PASSED: second
PASSED: third
===============================================
StackOverflow_50998867_test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
StackOverflow_50998867_suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
答案 1 :(得分:0)
到目前为止,在TestNG xml文件中不能具有“优先级”。
2017年出现了一个问题。所有者和合作伙伴已决定对此不采取任何措施,即状态为无法解决。< / strong>
您可以在此页面上参考问题:TestNG_Issue_1324
当前,优先级仅适用于@Test
,而preserve-order
仅适用于 xml 。
如果优先级比保留顺序更重要,则无法用xml覆盖顺序。
如果优先级不如保留顺序重要,则默认值必须更改为false,否则使用xml时它将破坏优先级(可能会更高,但测试应迅速将其突出显示)。