我的网站UI自动化测试用例很少。
如果第一次和第二次失败,我想尝试至少三次我的测试用例。这样,我想确保此测试用例始终失败。
如果我们在NUnit中有任何选择,请告诉我。我正在使用C#和NUnit。
答案 0 :(得分:26)
您可以根据属性repeat在nunit中添加新属性并重建库。这很简单。
[Test]
[Repeat( 25 )]
public void MyTest( ){
// your test logic here
}
答案 1 :(得分:7)
从NUnit 3.0开始,有一个“重试”属性看起来就像kumar想要的那样。
答案 2 :(得分:4)
对于逻辑循环,计算失败。断言。(失败== 0)轰隆完成。
不要这样做。写一个更好的测试,你可以证明是一致的。
答案 3 :(得分:2)
nunit使用4个例外来判断测试结果。它们是:TimeoutException,AssertException,SuccessException和IgnoreException。您可以在nunit测试函数中使用这些。我猜你可以把你的测试代码放在try-catch中,然后捕获nunit断言异常两次。但我应该说,正如之前所评论的那样,需要间歇性地失败的测试是重新思考的理由。
答案 4 :(得分:0)
如果您希望测试用例在报告失败之前多次运行,则可以使用“重试属性”。
[Test]
[Retry(3)]
public void MyTest( ){
// your test logic here
}
答案 5 :(得分:0)
我已经在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()