繁体   English   中英

在@BeforeSuite批注中获取类名-TestNG-Java

[英]Get the class name in the @BeforeSuite annotation - TestNG - Java

当不通过xml文件执行时,是否可以从@BeforeSuite批注内部获取脚本启动的类的名称?

这样做:

reportName = new Exception().getStackTrace()[0].getClassName();

返回包含@BeforeSuite批注和以下内容的类本身:

reportName = new Exception().getStackTrace()[1].getClassName();

返回sun.reflect.NativeMethodAccessorlmpl

如果我直接从单独的类执行脚本,那么我想获取该信息,因为我正在使用它来命名我的范围报告文件名。

如果您想知道@BeforeSuite批注中的代码是什么样的:

// Set Extent Report file name from the global properties file
String reportName = ctx.getCurrentXmlTest().getSuite().getName();
if (reportName.equals("Default suite"))
{
    reportName = new Exception().getStackTrace()[0].getClassName();
}
String timeStamp = new SimpleDateFormat("HH.mm.ss").format(new Date());

// Initialize Extent Reports and modify the looks/output
String extentReportPath = "";
if (extent == null) {
    if (os.equals("Mac"))
    {
        extentReportPath = reportPath + "/" + project + "-" + reportName + "-" + environment + "-" + browser + "-" + timeStamp + ".html";
    }
    else if (os.equals("Windows"))
    {
        extentReportPath = reportPath + "\\" + project + "-" + reportName + "-" + environment + "-" + browser + "-" + timeStamp + ".html";
    }

    // Start new report
    extent = new ExtentReports(extentReportPath, true);
}

还有更多内容,但这是与我的问题有关的部分。

-更新-

这是我的解决方案:

// Set Extent Report file name from the global properties file
String reportName = ctx.getCurrentXmlTest().getSuite().getName();
if (reportName.equals("Default suite"))
{
    List<ITestNGMethod> allMethods = ctx.getSuite().getAllMethods();
    for (ITestNGMethod method : allMethods)
    {
        String fullMethod = method.toString();
        int indexOf = fullMethod.indexOf(".");
        reportName = fullMethod.replace(fullMethod.substring(indexOf), "");             }
}

您可以将参数ITestContext传递给beforesuite方法。 testNG将自动注入它。 这应该包含您要查找的信息。

context.getSuite()。getAllMethods-> TestNGMethods.getRealClass()或getTestClass()的列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM