NUnit -重复测试用例3次,如果失败

本文关键字:如果 失败 3次 测试用例 -重 NUnit | 更新日期: 2023-09-27 18:05:16

我有几个Web站点UI自动化的测试用例。

如果第一次和第二次都失败了,我想要尝试我的测试用例至少三次。那样,我想要确保这个测试用例始终失败。

请让我知道,如果我们有任何选项在NUnit中使用。我正在使用c#和NUnit。

NUnit -重复测试用例3次,如果失败

您可以根据属性repeat在nunit中添加新属性并重新构建库。这很简单。

   [Test]
   [Repeat( 25 )]
   public void MyTest( ){
      // your test logic here
   }

从NUnit 3.0开始,有一个'Retry'属性看起来就像kumar想要的那样。

请参见重试属性

   [Test]
   [Retry(3)]
   public void MyTest( ){
      // your test logic here
   }

For循环遍历逻辑,计数失败。断言。(失败== 0)Boom done。

我建议你不要这样做。编写一个您可以证明是一致的更好的测试。

nunit使用4种异常来告诉测试结果。它们是:TimeoutException, AssertException, SuccessException和IgnoreException。您可以在非单元测试函数中使用这些函数。我猜你可以把你的测试代码放在try-catch中,并捕获nunit断言异常两次。但是我应该说,正如之前所评论的那样,间歇性失败测试的需求是重新考虑的理由。

如果您希望您的测试用例在报告失败之前运行多次,您可以使用Retry Attribute。

[Test]
[Retry(3)]
public void MyTest( ){
  // your test logic here
}

我用python为nunit3-console创建了一个包装脚本,该脚本运行该脚本,如果有任何失败的测试,将重新运行失败的测试多达--max-retries次。它还将把新的结果合并到原来的TestResult.xml中。

该脚本名为nunit-repeat,托管在https://github.com/sashoalm/nunit-repeat/.

使用例子:

python nunit-repeat.py --max-retries 3 -- .'MockFailingTests'packages'NUnit.ConsoleRunner.3.10.0'tools'nunit3-console.exe .'MockFailingTests'MockFailingTests'bin'Debug'MockFailingTests.dll
代码:

# Script to run nunit tests and then repeat any failed tests until we run out of retries.
import os
import sys
import subprocess
import xml.etree.ElementTree as ET
# Finds the corresponding test with the same name in another XML file.
def findCorrespondingTest(tree2, failedTest):
    test2 = tree2.findall(".//test-case[@fullname='{}']".format(failedTest.get('fullname')))
    if len(test2) != 1:
        print(failedTest.get('fullname'))
        for ii in test2:
            print(ii.get('fullname'))
        raise Exception("len(test2) != 1")
    return test2[0]
# Python XML lib doesn't show you who is the parent of a given element so we add the info ourselves.
def addParentInfo(tree):
    for child in tree:
        setParent(child, tree)
        addParentInfo(child)
# We strip the parentage info so it doesn't pollute the XML file.
def stripParentInfo(tree):
    for child in tree:
        child.attrib.pop('__my_parent__', 'None')
        stripParentInfo(child)
# Gets the parent of a given element.
def getParent(tree):
    return tree.attrib['__my_parent__']
# Sets the parent of a given element.
def setParent(tree, parent):
    tree.attrib['__my_parent__'] = parent
# Updates a parent test suite of a failed test that has passed on rerun.
def updateTestSuite(tree):
    tree.attrib['failed'] = str(int(tree.attrib['failed']) - 1)
    tree.attrib['passed'] = str(int(tree.attrib['passed']) + 1)
    if tree.attrib['failed'] == '0':
        tree.attrib['result'] = 'Passed'
# In-place replaces a failed test with a successful one.
def replaceTest(tree1, tree2):
    parent = getParent(tree1)
    tree1.__setstate__(tree2.__getstate__())
    setParent(tree1, parent)
# Updates the entire chain of parent test suites.
def updateParentTestSuites(testCase):
    suite = getParent(testCase)
    while suite and suite.tag == 'test-suite':
        updateTestSuite(suite)
        suite = getParent(suite)
# Merges the results from a rerun into the original test.
# Any failed test that has become successful upon rerun is updated.
def mergeRerunResults(tree1, tree2):
    for failedTest in tree1.iterfind(".//test-case[@result='Failed']"):
        test2 = findCorrespondingTest(tree2, failedTest)
        if test2.attrib['result'] == 'Passed':
            replaceTest(failedTest, test2)
            updateParentTestSuites(failedTest)
# Checks whether we have any failed tests.
def hasFailedTests(tree):
    return len(tree.findall(".//test-case[@result='Failed']")) > 0
# Writes the failed tests, one per line, in testlist.txt. This file
# will be passed to nunit console runner.
def writeFailedTests(tree):
    f = open('testlist.txt', 'w')
    for failedTest in tree.iterfind(".//test-case[@result='Failed']"):
        name = failedTest.attrib['fullname']
        f.write(name + ''n')
# Retries all the failing tests, until all pass or we run out of retries.
def retryFailedTests(args, retries):
    # Add the testfilter to nunit's command line.
    args.append('--testlist')
    args.append('testlist.txt')
    # Load the test results from the first invocation.
    tree = ET.parse('TestResult.xml')
    addParentInfo(tree.getroot())
    # Run the retries.
    while retries > 0 and hasFailedTests(tree):
        retries -= 1
        writeFailedTests(tree)
        subprocess.call(args)
        mergeRerunResults(tree, ET.parse('TestResult.xml'))
    # Write the final results.
    stripParentInfo(tree.getroot())
    tree.write('TestResult.xml')
    # Check if we still have failing tests.
    if hasFailedTests(tree):
        raise Exception("There are failed tests even after retrying.")
# Main function.
def main():
    args = sys.argv
    # Get the number of retries.
    try:
        retries = int(args[args.index('--max-retries') + 1])
    except:
        retries = 3
    # Get the nunit command line.
    args = args[args.index('--')+1:]
    # Invoke nunit the first time.
    subprocess.call(args)
    # Retry any failed tests.
    retryFailedTests(args, retries)
# Execute main function.
main()