【问题标题】:How to display a message in TestNG report (for passed test cases)如何在 TestNG 报告中显示消息(对于通过的测试用例)
【发布时间】:2023-03-04 21:58:01
【问题描述】:

TestNG 的默认电子邮件报告在测试用例失败或断言消息时显示异常。

但是在这里我想在通过的测试用例中添加一条消息,例如在 TestNG 报告中的一些输出值。具体来说:

1. Submit a form in UI.
2. A success message with ID will be displayed.
3. Retrieve that ID using selenium's getText().
4. In the TestNG report, print the above-retrieved ID as "Submitted form ID: {id}".

当用户点击报告通过的测试用例时,我希望显示这些消息。

【问题讨论】:

    标签: java selenium testng extentreports selenium-extent-report


    【解决方案1】:

    如果测试用例成功,您可以使用 Reporter.log 在 Testng 报告中显示消息,然后它将消息打印为“id 的值是”。

    参见下面的代码参考:

    @Test
    public void testCaseEnabling() {
       System.out.println("I'm Not Ready, please don't execute me");
    
       Assert.assertEquals(actual, expected);
    
       int id = 10;
    
       Reporter.log("the value of id is " + id);
    }
    

    还有:import org.testng.Reporter;

    【讨论】:

      【解决方案2】:

      您可以为此使用 Reporter.log 和 sys.out 的组合。

      我有这样的事情:

      import static util.Logging.log;
      public class PersonTest  {
      
         public void validatePersonName() {
            log("Creating new person.");
            Person person = createPerson();
      
            log("Getting person name.");
            String name = getPersonName(person);
            log("Person name is "+name+".");
      
            log("Validating person name.");
            validatePersonName(name);
            }
      
      }
      


      import org.testng.Reporter;
      public class LogUtils {
      
          public static void log(String log) {
              if (!logging) return;
      
              Reporter.log(log); // This is shown in the report.
              System.out.println("========== " + getTimestamp() + " - " + log); // This is shown in console.
          }
      
      }
      


      报告中的结果是这样的:


      失败的测试方法看起来像(由 testng 断言导致失败):

      我希望这是你需要的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-16
        • 1970-01-01
        • 1970-01-01
        • 2018-11-23
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多