首页 > 解决方案 > 质量保证 | 在 TestNg 监听器中获取场景标签

问题描述

我想在 TestNg 侦听器中获取 Scenario 标签详细信息,以帮助使用 TestcaseId 和 IntId 为每个场景编写自定义报告。

 @priority:1 @TestcaseId:VFF-265 @IntId:615d440932e941018806cfc5  @sit-1 
 Scenario : Testing login is successfull

我实施的当前解决方法是在场景名称中添加详细信息并在 TestNg 侦听器中使用 result.getTestName() 并将详细信息与场景名称分开。此实现的唯一问题是内部 ID 和 TC id 可见并使测试用例名称变长:

 @priority:1 @TestcaseId:VFF-265 @IntId:615d440932e941018806cfc5  @sit-1 
 Scenario : [VFF-265]_[615d440932e941018806cfc5]_Testing login is successfull

标签: testnglistenerqaf

解决方案


When you are using qaf with TestNG runner, it will use TestNGScenario as ITestNGMethod implementation. TestNGScenario is extended TestNGMethod which has addition method getMetaData() which will give you meta data of the test case. Any method from testng returning ITestNGMethod or TestNGMethod will return TestNGScenario object. Below is example

public void onTestSuccess(ITestResult tr) {
    TestNGScenario scenario = (TestNGScenario) tr.getMethod();
    Map<String, Object> metadata = scenario.getMetaData()
}

If you are working on custom reporting, best way is to implement TestCaseResultUpdator. You can refer qaf inbuilt reporters and qaf-support-elasticsearch.


推荐阅读