如何使用 "dependsOnMethods" 以编程方式查找已触发当前测试方法的测试方法名称
How to programmatically find the Test method name which has triggered the current test method using "dependsOnMethods"
我正在使用 TestNG 和注释- "dependsonMethods"
代码示例:
@Test(dependsOnMethods = { "test2" })
public void Test1() throws IOException { }
现在,需要以编程方式获取已触发其他测试方法 (Test2) 的测试方法 (Test1) 的名称。
******************更新了上下文和代码示例********************* *****
我正在 运行宁 TestNg,java,范围报告 4.0.9,Maven 项目中的硒测试。
CURRENT:测试 [addAndPopulateTest],它使用 TestNg-"dependsonMethods" 调用 [addTest]。在 运行 之后,范围报告将 [addAndPopulateTest] 和 [addTest] 的结果分别显示为 2 个单独的测试。这是link目前的情况:https://imgur.com/ZnBNqzo
EXPECTED:范围 HTML 报告应显示 - 单个测试,即 [addAndPopulateTest] 和 2 个子测试,即 [addTest] 和 [addAndPopulateTest]。这是 ToBe 的 link 情况:https://imgur.com/NKMxoIB
*****代码示例
///TC_addAndPopulateTest
package com.tra.testCases;
import org.testng.annotations.Test;
import com.tram.pageObjects.BaseClass;
public class TC_addAndPopulateTest extends TC_addTest {
@Test(dependsOnMethods = { "addTest" })
public void addAndPopulateTest() throws IOException, InterruptedException {
System.out.println("Currently executing Test-> addAndPopulateTest");
}
}
///TC_addTest
package com.tram.testCases;
import java.io.IOException;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.tram.pageObjects.BaseClass;
public class TC_addTest extends BaseClass {
@Test()
public void addTest() throws IOException, InterruptedException {
System.out.println("Currently executing Test->'addTest'");
}
}
///报告Class
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.tram.pageObjects.BaseClass;
public class Reporting extends BaseClass implements ITestListener {
private static ExtentReports extent =
ExtentManager.createInstance("Extent-Report.html");
private static ThreadLocal parentTest = new ThreadLocal();
private static ThreadLocal test = new ThreadLocal();
@Override
public synchronized void onStart(ITestContext context) {
}
@Override
public synchronized void onFinish(ITestContext context) {
extent.flush();
}
@Override
public synchronized void onTestStart(ITestResult result) {
**//Currently, [addTest] is ready to be executed, So, need to the find
that this test method [addTest] has triggered directly by [TC_addTest]
class or by other test method.
A] If directly by the test class, set
PARENT=result.getMethod().getMethodName()
B] if indirectly by the other test method, set PARENT= Name of the test
method [addAndPopulateTest] which has triggered the [addTest] //**
ExtentTest parent =
Extent.createTest(result.getMethod().getMethodName());
parentTest.set(parent);
}
@Override
public synchronized void onTestSuccess(ITestResult result) {
ExtentTest child = ((ExtentTest)
parentTest.get()).createNode(result.getMethod().getMethodName());
test.set(child);
((ExtentTest) test.get()).pass("Test passed");
}
@Override
public synchronized void onTestFailure(ITestResult result) {
((ExtentTest) test.get()).fail(result.getThrowable());
}
@Override
public synchronized void onTestSkipped(ITestResult result) {
((ExtentTest) test.get()).skip(result.getThrowable());
}
@Override
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult
result) {
}
}
//扩展管理器
package com.tram.utilities;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentManager {
private static ExtentReports extent;
public static ExtentReports getInstance() {
if (extent == null)
createInstance("test-output/extent.html");
return extent;
}
public static ExtentReports createInstance(String fileName) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setReportName(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
return extent;
}
}
检测运行ning测试的方法名称的一种简单方法是将java.lang.reflect.Method
作为参数注入方法级配置(@BeforeMethod
或@AfterMethod
).
作为 TestNG 中依赖注入功能的一部分,Method
的实例被注入 @BeforeMethod
或 @AfterMethod
"will receive the test method that will be called once this @BeforeMethod finishes (or after the method as run for @AfterMethod)"。 (参见 Native Dependency Injection)
这将允许我们检索正在执行的任何方法的名称,甚至是依赖测试方法,随后可以将其传递给报告。
作为考虑到您的用例的简短示例,请考虑以下 class:
public class MethodNameDemo {
String methodName;
@BeforeMethod
public void setup(Method method) {
methodName = method.getName();
}
@Test
public void test2() {
System.out.println("Running " + methodName);
}
@Test (dependsOnMethods = "test2")
public void test1() {
System.out.println("Running " + methodName);
}
}
当我们直接运行test1()
方法时,dependsOnMethods
会强制test2
先执行。 @BeforeMethod
会在每个测试方法前运行,将class字段methodName
赋值给即将执行的测试方法的值
执行test1()
后,我们将得到以下输出:
Running test2
Running test1
获得这些值后,您就可以执行任何需要的操作以传递给 Extent。
最后,通过实施 "ITestListener".
比较实例名称与方法名称来解决我的问题
A) 如果实例名称 = 方法名称,则使用方法名称创建测试。
B) 如果实例名=!方法名,使用实例名创建测试。
public static ArrayList<String> mylist = new ArrayList<String>();
@Override
public void onTestStart(ITestResult result) {
String instance = result.getInstanceName().toString();
String methodname = result.getMethod().toString();
if ((chkAndAddArray(finalInstance))) {
return;
}
if (finalmethodname.equalsIgnoreCase(finalInstance.toString())) {
logger = extent.createTest(finalmethodname);
} else {
logger = extent.createTest(finalInstance);
}
}
boolean chkAndAddArray(String instance) {
if (mylist.contains(instance)) {
System.out.println(instance + " value is already present");
return true;
} else
mylist.add(instance);
System.out.println(instance + " value is not present & now, added");
return false;
}
我正在使用 TestNG 和注释- "dependsonMethods"
代码示例:
@Test(dependsOnMethods = { "test2" })
public void Test1() throws IOException { }
现在,需要以编程方式获取已触发其他测试方法 (Test2) 的测试方法 (Test1) 的名称。
******************更新了上下文和代码示例********************* *****
我正在 运行宁 TestNg,java,范围报告 4.0.9,Maven 项目中的硒测试。
CURRENT:测试 [addAndPopulateTest],它使用 TestNg-"dependsonMethods" 调用 [addTest]。在 运行 之后,范围报告将 [addAndPopulateTest] 和 [addTest] 的结果分别显示为 2 个单独的测试。这是link目前的情况:https://imgur.com/ZnBNqzo
EXPECTED:范围 HTML 报告应显示 - 单个测试,即 [addAndPopulateTest] 和 2 个子测试,即 [addTest] 和 [addAndPopulateTest]。这是 ToBe 的 link 情况:https://imgur.com/NKMxoIB
*****代码示例
///TC_addAndPopulateTest
package com.tra.testCases;
import org.testng.annotations.Test;
import com.tram.pageObjects.BaseClass;
public class TC_addAndPopulateTest extends TC_addTest {
@Test(dependsOnMethods = { "addTest" })
public void addAndPopulateTest() throws IOException, InterruptedException {
System.out.println("Currently executing Test-> addAndPopulateTest");
}
}
///TC_addTest
package com.tram.testCases;
import java.io.IOException;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.tram.pageObjects.BaseClass;
public class TC_addTest extends BaseClass {
@Test()
public void addTest() throws IOException, InterruptedException {
System.out.println("Currently executing Test->'addTest'");
}
}
///报告Class
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.tram.pageObjects.BaseClass;
public class Reporting extends BaseClass implements ITestListener {
private static ExtentReports extent =
ExtentManager.createInstance("Extent-Report.html");
private static ThreadLocal parentTest = new ThreadLocal();
private static ThreadLocal test = new ThreadLocal();
@Override
public synchronized void onStart(ITestContext context) {
}
@Override
public synchronized void onFinish(ITestContext context) {
extent.flush();
}
@Override
public synchronized void onTestStart(ITestResult result) {
**//Currently, [addTest] is ready to be executed, So, need to the find
that this test method [addTest] has triggered directly by [TC_addTest]
class or by other test method.
A] If directly by the test class, set
PARENT=result.getMethod().getMethodName()
B] if indirectly by the other test method, set PARENT= Name of the test
method [addAndPopulateTest] which has triggered the [addTest] //**
ExtentTest parent =
Extent.createTest(result.getMethod().getMethodName());
parentTest.set(parent);
}
@Override
public synchronized void onTestSuccess(ITestResult result) {
ExtentTest child = ((ExtentTest)
parentTest.get()).createNode(result.getMethod().getMethodName());
test.set(child);
((ExtentTest) test.get()).pass("Test passed");
}
@Override
public synchronized void onTestFailure(ITestResult result) {
((ExtentTest) test.get()).fail(result.getThrowable());
}
@Override
public synchronized void onTestSkipped(ITestResult result) {
((ExtentTest) test.get()).skip(result.getThrowable());
}
@Override
public synchronized void onTestFailedButWithinSuccessPercentage(ITestResult
result) {
}
}
//扩展管理器
package com.tram.utilities;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentManager {
private static ExtentReports extent;
public static ExtentReports getInstance() {
if (extent == null)
createInstance("test-output/extent.html");
return extent;
}
public static ExtentReports createInstance(String fileName) {
ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(fileName);
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setReportName(fileName);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
return extent;
}
}
检测运行ning测试的方法名称的一种简单方法是将java.lang.reflect.Method
作为参数注入方法级配置(@BeforeMethod
或@AfterMethod
).
作为 TestNG 中依赖注入功能的一部分,Method
的实例被注入 @BeforeMethod
或 @AfterMethod
"will receive the test method that will be called once this @BeforeMethod finishes (or after the method as run for @AfterMethod)"。 (参见 Native Dependency Injection)
这将允许我们检索正在执行的任何方法的名称,甚至是依赖测试方法,随后可以将其传递给报告。
作为考虑到您的用例的简短示例,请考虑以下 class:
public class MethodNameDemo {
String methodName;
@BeforeMethod
public void setup(Method method) {
methodName = method.getName();
}
@Test
public void test2() {
System.out.println("Running " + methodName);
}
@Test (dependsOnMethods = "test2")
public void test1() {
System.out.println("Running " + methodName);
}
}
当我们直接运行test1()
方法时,dependsOnMethods
会强制test2
先执行。 @BeforeMethod
会在每个测试方法前运行,将class字段methodName
赋值给即将执行的测试方法的值
执行test1()
后,我们将得到以下输出:
Running test2
Running test1
获得这些值后,您就可以执行任何需要的操作以传递给 Extent。
最后,通过实施 "ITestListener".
比较实例名称与方法名称来解决我的问题A) 如果实例名称 = 方法名称,则使用方法名称创建测试。
B) 如果实例名=!方法名,使用实例名创建测试。
public static ArrayList<String> mylist = new ArrayList<String>();
@Override
public void onTestStart(ITestResult result) {
String instance = result.getInstanceName().toString();
String methodname = result.getMethod().toString();
if ((chkAndAddArray(finalInstance))) {
return;
}
if (finalmethodname.equalsIgnoreCase(finalInstance.toString())) {
logger = extent.createTest(finalmethodname);
} else {
logger = extent.createTest(finalInstance);
}
}
boolean chkAndAddArray(String instance) {
if (mylist.contains(instance)) {
System.out.println(instance + " value is already present");
return true;
} else
mylist.add(instance);
System.out.println(instance + " value is not present & now, added");
return false;
}