如何优化 testng 和 seleniums 测试
在我的实习中,我必须使用 TestNG 和 selenium 来测试 Web 应用程序。但我有一个问题,有时硒或浏览器由于某种随机原因无法工作,因此工作测试被标记为“失败”。为了避免这种情况,我可以使用注释@Test(invocalCount = 4, successPercentage = 25)
,那么如果测试成功一次,则测试被标记为“成功”,这很好,但有问题就是这个方案将测试时间乘以4,效率不是很高。
我可以做的来减少测试时间,就是编写一些规则“如果测试失败,则重新运行此测试(并且仅当测试失败时),如果第二次、第三次或第四次有效,则将此测试标记为“成功”“这样我就可以避免这些随机错误。但我还没有找到如何编写这个规则,我看到我们可以添加一个监听器,所以我们有一个名为“onTestFailure
”的方法,这样我可以在测试失败时做一些事情,但我不这样做不知道如何重新运行测试。
我还发现 testng-failed.xml 保存了所有失败的测试,因此我们可以运行此 xml 文件来重新运行这些测试,但这会删除之前第一次运行的报告,但我只想标记失败的测试如果第二次运行成功,则为“成功”。 (我已经将 testNG/selenium 集成到 Jenkins 中,所以我有一个包含所有测试的图表,所以这个方法不是很适应,但是这个方法不会将测试时间乘以 4,这就是我想要的)
所以如果你有任何关于如何做到这一点的线索,那就太好了。
For my internship, I have to use TestNG and selenium for testing a web-application. But I have a problem, sometimes selenium or the Browser is not working for some random reason, so a working test is marked as "failed". To avoid that, I can use the annotation @Test(invocationCount = 4, successPercentage = 25)
, then if the test succeeds one time, the test is marked as "Succeed", that's good but the problem is that this solution multiply the time for testing by 4, this is not very efficient.
What I can do to decrease the time for testing, is to write some rule "if the test failed, rerun this test (and only if the test has failed), and if it worked the second, third, or the fourth time, then mark this test as "succeed" " So I can avoid these random bugs. But I've not found how to write this rule, I saw that we can add a listener, so we have a method called "onTestFailure
" so I can do something when the test has failed but I don't know how to re-run the test.
I also found testng-failed.xml where all the failed tests are saved, so we can run this xml file for rerun these tests, but this will erase the report from the previous first run, but I want just that the failed tests are marked as "succeed" if the second run is successful. (I have integrated testNG/selenium to Jenkins, so I have a graph with all tests, so this method is not very adapted, but this method don't multiply the time for testing by 4 and this is what I want)
So if you have any clue for how to do that, it would be very nice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 IRetryAnalyzer、侦听器和自定义报告器的组合来完成您想要的操作。
IRetryAnalyzer:
RetryListener:
但是,TestNG 中似乎存在一个错误,实际上导致某些测试结果被报告为跳过和失败。为了防止这种情况,我建议您重写您想要使用的任何 Reporter,并包含如下方法:
完成所有这些操作后,您可以使用 .addListener 添加报告器,并在 @Test 中指定 retryAnalyzer注解。
You can use a combination of the IRetryAnalyzer, a Listener and a custom reporter to do what you are looking for.
IRetryAnalyzer:
RetryListener:
However, there appears to be a bug within TestNG that actually causes some of the test results to be reported as both skipped AND failed. To prevent this, I recommend that you override whatever Reporter you wish to use and include a method such as the one included below:
After all of this is done, you can add the reporter using using .addListener and specify the retryAnalyzer in the @Test annotation.
您不需要实现 onTestFailure。当测试失败时,TestNG 自动调用重试。所以不需要重写onTestFailure。这会导致重试方法 2 调用。
我按如下方式实现了重试。
在上面的线程中,由于 onTestFailure 的实现,重试被调用两次。我删除了失败的结果,以便当您重试时它会使用最新的结果。否则,如果您有依赖性测试方法,它将被跳过(尽管在重试时它会通过,因为它使用第一个结果)。您可能必须在报告时处理失败的重试结果。
您可能必须像这样重试后删除正在通过的测试。
希望它有助于更好地理解重试。
You need not implement onTestFailure. TestNG calls retry automatically when test fails. So no need to override onTestFailure. this causes retry method 2 calls.
I implemented retry as below.
In the above thread retry getting called twice because of implementation of onTestFailure. I remove the failed results so that when you retry It uses latest result. Otherwise if you have dependency test method it get skipped(though on retry it passed as it uses first result).You might have to handle failed retried results while reporting.
You might have to remove the tests that are passing after retry like this.
Hope it helps to understand retry better.
其他答案是“正确”的方式。对于“快速而肮脏”的方法,只需将实际的测试代码移至私有方法即可。在带注释的测试方法中,在查找 Throwable(所有错误和异常的超类)的位置中创建一个带有 try..catch 的 for 循环。出错时只需继续循环或抛出最后一个错误,成功时则中断循环。
示例代码:
不过,通常最好编写不会随机失败的测试。同样使用这种方法,您不会获得有关失败次数的信息,这毕竟是一个重要信息。我个人宁愿在失败时在 Jenkins 上重新运行整个测试类/套件,并调查失败的原因。
The other answers are the "right" way. For a "quick'n'dirty" way just move the actual test code to a private method. In your annotated test method, make a for-loop with a try..catch inside where you look for Throwable (superclass of all errors and exceptions). On error just continue loop or throw the last error, on success break loop.
Sample code:
Usually, though, it's better to write tests that do not fail randomly. Also using this approach you don't have information about how many tries failed, which is an important information after all. I personally rather re-run the whole test class/suite on Jenkins on failure, and investigate why it failed.