问题描述
我正在使用 TestNG 运行自动化测试.我还实现了 TestNG Listener 以某种格式保存测试结果.
I'm using TestNG to run automation tests. I also implemented TestNG Listener to save the test results in a certain format.
对于某些特定场景,我需要执行以下操作:当测试运行时,一个值被分配给一个变量(局部方法变量).我需要将变量的值传递给我实现的 TestNG Listener 类.有多种方法(ontestStart、onTestFinish、onTestSuccess/Failure/Skipped),但我无法弄清楚如何在运行时获取变量.我在 TestNG 文档中没有找到它.
For some specific scenario I need to do the following:When a test runs, a value is assigned to a variable (local method variable).I need to pass the value of the variable to the TestNG Listener class I implemented. There are various methods (ontestStart, onTestFinish, onTestSuccess/Failure/Skipped), but I cannot figure out how to get the variable at run time.And I didn't find it in TestNG documentation.
有人可以帮忙吗?或者分享他/她在类似问题上的经验.
Can anyone please help? Or share his/her experience with a similar issue.
谢谢!
推荐答案
你可以试试通过 监听方法调用并将值设置到 [ITestResult
](http://testng.org/javadocs/org/testng/IAttributes.html#setAttribute(java.lang.String, java.lang.Object)).然后您会发现 ITestResult
中的值到 您的自定义记者.
You can try by listening to the method invocations and set values into the [ITestResult
](http://testng.org/javadocs/org/testng/IAttributes.html#setAttribute(java.lang.String, java.lang.Object)).Then you'll find the values from ITestResult
into your custom reporter.
这篇关于如何使用 TestNG 侦听器从测试方法中获取变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!