简体   繁体   English

如何在Jenkins控制台输出中显示测试用例详细信息?

[英]How to display Test Cases details in Jenkins Console Output?

I want to display Excel(XLSX) Test cases id,description and status while running selenium testng execution in the Jenkins Console output,below is the format. 我想在Jenkins Console输出中运行硒testng执行时显示Excel(XLSX)测试用例ID,说明和状态,以下是格式。

TC_ID|Test Cases Description|Status
TC01|Verify Login Home|PASS
TC02|Verify Login Screen|FAIL

In case any one did this type of model please help me to achieve this in Jenkins Output Console. 如果有人做过这种类型的模型,请帮助我在Jenkins输出控制台中实现。

If you are using TestNG in your project, anything you log using Reporter class will be also printed in Jenkins console output : 如果您在项目中使用TestNG,则使用Reporter类记录的所有内容也会在Jenkins控制台输出中打印:

Reporter.log("This will be displayed in the TestNG report AND in Jenkins console");

Note : You need to add the following import : 注意:您需要添加以下导入:

import org.testng.Reporter;

Refer this link for steps to create a TestNG project in Jenkins. 请参阅链接以获取在Jenkins中创建TestNG项目的步骤。 Alternatively, you can also as well create a Maven project where your pom file will execute your TestNG tests. 另外,您也可以创建一个Maven项目,其中的pom文件将执行TestNG测试。

Now, if you want to get the data from an excel file, you can use Apache POI . 现在,如果要从excel文件中获取数据,则可以使用Apache POI Refer this link for an example to read data from excel. 请参阅链接以获取从excel读取数据的示例。

If you want PASS, FAIL status to be printed beside your test case name, you can use ITestResult . 如果要在测试用例名称旁边打印通过,失败状态,则可以使用ITestResult Suppose you are printing the data in your @AfterMethod method, you would do something like - 假设您要在@AfterMethod方法中打印数据,则应执行以下操作-

@AfterMethod
public void afterSuite(ITestResult result)
{
  if(result.getStatus() == ITestResult.SUCCESS)
    {
      //pass
    }
  else if(result.getStatus() == ITestResult.FAILURE)
    {
            //fail
    }

  else if(result.getStatus() == ITestResult.SKIP )
    {    
             //skip
    }
}

Now, you can store the each respective test method result along with the name you read from the excel file, store it in a Map and then print it one by one in your @AfterSuite method. 现在,您可以将每个测试方法的结果与从excel文件中读取的名称一起存储,将其存储在Map中,然后在@AfterSuite方法中逐一打印。

Maybe will be better to implement listener but you can use code below: 也许实现监听器会更好,但是您可以使用以下代码:

@AfterMethod(alwaysRun = true)
public void beforeMethod(Method test, ITestResult testResult) {

    //get name of test. if testName annotation is empty get method name.
    String name = firstNonEmpty(
            test.getDeclaredAnnotation(org.testng.annotations.Test.class).testName(),
            test.getName()).get();

    String description = test.getDeclaredAnnotation(org.testng.annotations.Test.class).description();

    //get test result
    String result = "UNKNOWN";
    switch (testResult.getStatus()) {
        case 1:
            result = "SUCCESS";
            break;
        case 2:
            result = "FAILURE";
            break;
        case 3:
            result = "SKIP";
            break;
        case 4:
            result = "SUCCESS_PERCENTAGE_FAILURE";
            break;
        case 16:
            result = "STARTED";
            break;
    }

    //report as you wish..
    System.out.println(String.format("%s|%s|%s", name, description, result));
}

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

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