本文介绍了如何优化testng和seleniums测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的实习,我必须使用TestNG和selenium来测试网络应用程序。但我有一个问题,有时候selenium或浏览器因某些随机原因无法工作,因此工作测试被标记为失败。为了避免这种情况,我可以使用注释 @Test(invocationCount = 4,successPercentage = 25),然后如果测试成功一次,则测试标记为成功 ,这很好,但问题是这个解决方案将测试时间乘以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.

我可以做些什么来减少测试时间,就是写一些规则如果测试失败,重新运行此测试(并且只有在测试失败时),如果它在第二次,第三次或第四次工作,那么将此测试标记为成功所以我可以避免这些随机错误。但我还没有找到如何编写这个规则,我看到我们可以添加一个监听器,所以我们有一个名为 onTestFailure 的方法,所以我可以做一些事情测试失败但我不知道如何重新运行测试。

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.

我还发现了testng-failed.xml,其中保存了所有失败的测试,因此我们可以运行这个xml文件来重新运行这些测试,但是这将删除之前的第一次运行,但我只想在第二次运行成功时将失败的测试标记为成功。 (我已将testNG / selenium集成到Jenkins中,所以我有一个包含所有测试的图表,所以这个方法不是很适应,但这种方法不会将测试时间乘以4,这就是我想要的)

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.

推荐答案

您无需实现onTestFailure。测试失败时,TestNG会自动重试。所以不需要覆盖onTestFailure。这会导致重试方法2调用。
我实现了如下重试。

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.


private final Map rerunCountForTesCase = new HashMap();
    @Override
    public boolean retry(ITestResult result) {
                // here i have unique test case IDs for each of test method. 
                // So using utility method to get it. You can use method calss+name combination instead of testcaseid like me
        String executingTestCaseID = ReportingUtilities.getTestCaseId(result);
        if(rerunCountForTesCase.containsKey(executingTestCaseID))
        {
            count = rerunCountForTesCase.get(executingTestCaseID);
        }
        else
        {
            rerunCountForTesCase.put(executingTestCaseID, 0);
        }
        if (count 0)
                {
                    logInfo(tcID,"Sleeping for "+timeInSecs+ " secs before rerunning the testcase");
                    Thread.sleep(timeInSecs * CommonFwBase.SHORTWAIT );
                }
            } catch (InterruptedException e) {
                logError(null, e.getMessage());

            }

            rerunCountForTesCase.put(executingTestCaseID, ++count);
            return true;
        }
        return false;

    }

在上面的线程重试中被调用两次因为onTestFailure的实现。我删除失败的结果,以便在您重试时使用最新结果。否则,如果你有依赖测试方法,它会被跳过(虽然重试它会因为它使用第一个结果而传递)。你可能必须在报告时处理失败的重试结果。
您可能必须删除重试后传递的测试。

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.

        m_ptests = suiteTestContext.getPassedTests();
        m_ftests = suiteTestContext.getFailedTests();
        m_stests = suiteTestContext.getSkippedTests();

        List<ITestNGMethod> methodsToRemove = new ArrayList<ITestNGMethod>();

        for(ITestResult failed_result : m_ftests.getAllResults())
        {
            String failed_testName = failed_result.getMethod().getMethodName();
            String failingTest_className = failed_result.getClass().getName();
            for(ITestResult passed_result : m_ptests.getAllResults())
            {
                String passing_testName = passed_result.getMethod().getMethodName();
                String passingTest_className = failed_result.getClass().getName();
                if(failed_testName.equals(passing_testName) &&  
                        passingTest_className.equals(failingTest_className) ))
                {
                    if(passed_result.getEndMillis() > failed_result.getEndMillis())
                    {

                        methodsToRemove.add(failed_result.getMethod());
                        break;
                    }

                }
            }
        }

        // remove the test that passed on retry
        for(ITestNGMethod failedMethodToRemove : methodsToRemove)
        {
            m_ftests.removeResult(failedMethodToRemove);
        }

希望更好地了解重试有用。

Hope it helps to understand retry better.

这篇关于如何优化testng和seleniums测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 23:03