如何覆盖 testNG 中的 index.html 报告
How to override the index.html report in testNG
我有一个场景需要将一些自定义消息添加到 index.html testNG 报告中。有什么办法吗?
我刚刚创建了一个自定义注释,我想像 DataProvider 那样将其发布到 index.html testNG 报告中。到目前为止,我已经尝试过以下代码。
以下 class 将创建注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Greet {
/**
* @return - The name of the person to greet.
*/
String name() default "";
}
我只是用谷歌搜索但不知道数据提供者如何将测试数据发布到默认的 TestNG 报告中。如果有人了解数据提供者的内部逻辑,请告诉我。如果有任何文档可以更好地理解这一点,我们将不胜感激。
我刚刚创建了一个自定义注释,我想像 DataProvider 那样将其发布到 index.html testNG 报告中。到目前为止,我已经尝试过以下代码。
以下 class 将创建注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Greet {
/**
* @return - The name of the person to greet.
*/
String name() default "";
}
以下class将从用户获取数据:
public class TestCase1 {
@Test
@DataPublish(name="First Test method_1")
public static void test1() throws Exception {
try {
Assert.assertTrue(true);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
我想在 testNG index.html 报告中打印该注释值。
我认为您正在尝试更改 index.html 报告。您可以在面板 classes 中拥有任何数据并在 index.html.
中打印它
为此,您需要更改三个文件(classes)。所有 class 都是 here
Main.java
TimesPanel.java
(此 class 将取决于您要更改的内容(面板)。为了解释目的,我们将在报告的信息部分下的时间面板中添加内容,因此 TimesPanel.java
)
和BaseMultiSuitePanel.java
在您的项目中创建一个文件customBaseMultiSuitePanel.java
,复制原始文件的内容并相应地更改构造函数。
创建 customTimesPanel.java
并复制 TimesPanel.java
的内容并更改 private String js(ISuite suite)
方法,因为我们将向 table 添加 successPercentage 和测试优先级出现,当您点击报告中的次数时。
public class customTimesPanel extends customBaseMultiSuitePanel {
...
...
private String js(ISuite suite) {
String functionName = "tableData_" + suiteToTag(suite);
StringBuilder result = new StringBuilder(
"suiteTableInitFunctions.push('" + functionName + "');\n"
+ "function " + functionName + "() {\n"
+ "var data = new google.visualization.DataTable();\n"
+ "data.addColumn('number', 'Number');\n"
+ "data.addColumn('string', 'Method');\n"
+ "data.addColumn('string', 'Class');\n"
+ "data.addColumn('number', 'Time (ms)');\n"
+ "data.addColumn('string', 'SuccessPercentage');\n"
+ "data.addColumn('string', 'Priority');\n");
List<ITestResult> allTestResults = getModel().getAllTestResults(suite);
result.append(
"data.addRows(" + allTestResults.size() + ");\n");
Collections.sort(allTestResults, new Comparator<ITestResult>() {
@Override
public int compare(ITestResult o1, ITestResult o2) {
long t1 = o1.getEndMillis() - o1.getStartMillis();
long t2 = o2.getEndMillis() - o2.getStartMillis();
return (int) (t2 - t1);
}
});
int index = 0;
for (ITestResult tr : allTestResults) {
ITestNGMethod m = tr.getMethod();
long time = tr.getEndMillis() - tr.getStartMillis();
result
.append("data.setCell(" + index + ", "
+ "0, " + index + ")\n")
.append("data.setCell(" + index + ", "
+ "1, '" + m.getMethodName() + "')\n")
.append("data.setCell(" + index + ", "
+ "2, '" + m.getTestClass().getName() + "')\n")
.append("data.setCell(" + index + ", "
+ "3, " + time + ")\n")
.append("data.setCell(" + index + ", "
+ "4, '" + m.getSuccessPercentage() + "')\n")
.append("data.setCell(" + index + ", "
+ "5, '" + m.getPriority() + "');\n");
Long total = m_totalTime.get(suite.getName());
if (total == null) {
total = 0L;
}
m_totalTime.put(suite.getName(), total + time);
index++;
...
...
}
下一步,创建customIndexHtmlReport.java
并复制Main.java
的内容到这个文件中。在 public void generateReport()
方法
中删除旧的 TimesPanel 对象和下面的新对象
new customTimesPanel(m_model)
也像这样在同一个文件中更改报告名称
Utils.writeUtf8File(m_outputDirectory, "customReport-index.html", xsb, all);
最后,将侦听器添加到您的 testng.xml
<listener class-name = "firsttestngpackage.customIndexHtmlReport" />
然后您将获得如下所示的自定义报告,其中添加了每个测试的成功百分比和优先级
注:
确保与 customIndexHtmlReport.java
中的 getClass().getResourceAsStream
方法有关的资源已正确放置在您的项目中。我遇到了问题。
我有一个场景需要将一些自定义消息添加到 index.html testNG 报告中。有什么办法吗?
我刚刚创建了一个自定义注释,我想像 DataProvider 那样将其发布到 index.html testNG 报告中。到目前为止,我已经尝试过以下代码。
以下 class 将创建注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Greet {
/**
* @return - The name of the person to greet.
*/
String name() default "";
}
我只是用谷歌搜索但不知道数据提供者如何将测试数据发布到默认的 TestNG 报告中。如果有人了解数据提供者的内部逻辑,请告诉我。如果有任何文档可以更好地理解这一点,我们将不胜感激。
我刚刚创建了一个自定义注释,我想像 DataProvider 那样将其发布到 index.html testNG 报告中。到目前为止,我已经尝试过以下代码。
以下 class 将创建注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface Greet {
/**
* @return - The name of the person to greet.
*/
String name() default "";
}
以下class将从用户获取数据:
public class TestCase1 {
@Test
@DataPublish(name="First Test method_1")
public static void test1() throws Exception {
try {
Assert.assertTrue(true);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
我想在 testNG index.html 报告中打印该注释值。
我认为您正在尝试更改 index.html 报告。您可以在面板 classes 中拥有任何数据并在 index.html.
中打印它为此,您需要更改三个文件(classes)。所有 class 都是 here
Main.java
TimesPanel.java
(此 class 将取决于您要更改的内容(面板)。为了解释目的,我们将在报告的信息部分下的时间面板中添加内容,因此 TimesPanel.java
)
和BaseMultiSuitePanel.java
在您的项目中创建一个文件customBaseMultiSuitePanel.java
,复制原始文件的内容并相应地更改构造函数。
创建 customTimesPanel.java
并复制 TimesPanel.java
的内容并更改 private String js(ISuite suite)
方法,因为我们将向 table 添加 successPercentage 和测试优先级出现,当您点击报告中的次数时。
public class customTimesPanel extends customBaseMultiSuitePanel {
...
...
private String js(ISuite suite) {
String functionName = "tableData_" + suiteToTag(suite);
StringBuilder result = new StringBuilder(
"suiteTableInitFunctions.push('" + functionName + "');\n"
+ "function " + functionName + "() {\n"
+ "var data = new google.visualization.DataTable();\n"
+ "data.addColumn('number', 'Number');\n"
+ "data.addColumn('string', 'Method');\n"
+ "data.addColumn('string', 'Class');\n"
+ "data.addColumn('number', 'Time (ms)');\n"
+ "data.addColumn('string', 'SuccessPercentage');\n"
+ "data.addColumn('string', 'Priority');\n");
List<ITestResult> allTestResults = getModel().getAllTestResults(suite);
result.append(
"data.addRows(" + allTestResults.size() + ");\n");
Collections.sort(allTestResults, new Comparator<ITestResult>() {
@Override
public int compare(ITestResult o1, ITestResult o2) {
long t1 = o1.getEndMillis() - o1.getStartMillis();
long t2 = o2.getEndMillis() - o2.getStartMillis();
return (int) (t2 - t1);
}
});
int index = 0;
for (ITestResult tr : allTestResults) {
ITestNGMethod m = tr.getMethod();
long time = tr.getEndMillis() - tr.getStartMillis();
result
.append("data.setCell(" + index + ", "
+ "0, " + index + ")\n")
.append("data.setCell(" + index + ", "
+ "1, '" + m.getMethodName() + "')\n")
.append("data.setCell(" + index + ", "
+ "2, '" + m.getTestClass().getName() + "')\n")
.append("data.setCell(" + index + ", "
+ "3, " + time + ")\n")
.append("data.setCell(" + index + ", "
+ "4, '" + m.getSuccessPercentage() + "')\n")
.append("data.setCell(" + index + ", "
+ "5, '" + m.getPriority() + "');\n");
Long total = m_totalTime.get(suite.getName());
if (total == null) {
total = 0L;
}
m_totalTime.put(suite.getName(), total + time);
index++;
...
...
}
下一步,创建customIndexHtmlReport.java
并复制Main.java
的内容到这个文件中。在 public void generateReport()
方法
new customTimesPanel(m_model)
也像这样在同一个文件中更改报告名称
Utils.writeUtf8File(m_outputDirectory, "customReport-index.html", xsb, all);
最后,将侦听器添加到您的 testng.xml
<listener class-name = "firsttestngpackage.customIndexHtmlReport" />
然后您将获得如下所示的自定义报告,其中添加了每个测试的成功百分比和优先级
注:
确保与 customIndexHtmlReport.java
中的 getClass().getResourceAsStream
方法有关的资源已正确放置在您的项目中。我遇到了问题。