-
20-08-2019 - |
题
我工作的一个科学计算代码(C++编写的),并且除了执行单位测试的小组,我想要做的回归测试的一些数字输出的比较到一个"良好"的回答从以前的修订。有几个特点,我想:
- 允许比较数字为指定的容忍(两舍入错误和宽松的期望)
- 辨别能力之间的整数,加倍等和忽略案文,如果必要
- 及格式的输出,以告诉什么是错误的,其中:在多列表中的数据,只有显示列入的不同之处在于
- 返回
EXIT_SUCCESS
或EXIT_FAILURE
根据文件是否匹配
是否有任何良好的剧本或应用有这样做,或者我会得到摇我自己在Python阅读和比较输出的文件?当然我不是第一个这样的要求。
[以下不是严格相关的,但它可以考虑的因素的决定要做什么。我用CMake和其嵌入CTest功能性来驱动单元的测试,利用谷歌测试框架。我想这应该不会很难添加一些 add_custom_command
在我的发言 CMakeLists.txt
叫什么回归软件我需要的。]
解决方案 3
我最后写的Python脚本来做更多或更少我想要的。
#!/usr/bin/env python
import sys
import re
from optparse import OptionParser
from math import fabs
splitPattern = re.compile(r',|\s+|;')
class FailObject(object):
def __init__(self, options):
self.options = options
self.failure = False
def fail(self, brief, full = ""):
print ">>>> ", brief
if options.verbose and full != "":
print " ", full
self.failure = True
def exit(self):
if (self.failure):
print "FAILURE"
sys.exit(1)
else:
print "SUCCESS"
sys.exit(0)
def numSplit(line):
list = splitPattern.split(line)
if list[-1] == "":
del list[-1]
numList = [float(a) for a in list]
return numList
def softEquiv(ref, target, tolerance):
if (fabs(target - ref) <= fabs(ref) * tolerance):
return True
#if the reference number is zero, allow tolerance
if (ref == 0.0):
return (fabs(target) <= tolerance)
#if reference is non-zero and it failed the first test
return False
def compareStrings(f, options, expLine, actLine, lineNum):
### check that they're a bunch of numbers
try:
exp = numSplit(expLine)
act = numSplit(actLine)
except ValueError, e:
# print "It looks like line %d is made of strings (exp=%s, act=%s)." \
# % (lineNum, expLine, actLine)
if (expLine != actLine and options.checkText):
f.fail( "Text did not match in line %d" % lineNum )
return
### check the ranges
if len(exp) != len(act):
f.fail( "Wrong number of columns in line %d" % lineNum )
return
### soft equiv on each value
for col in range(0, len(exp)):
expVal = exp[col]
actVal = act[col]
if not softEquiv(expVal, actVal, options.tol):
f.fail( "Non-equivalence in line %d, column %d"
% (lineNum, col) )
return
def run(expectedFileName, actualFileName, options):
# message reporter
f = FailObject(options)
expected = open(expectedFileName)
actual = open(actualFileName)
lineNum = 0
while True:
lineNum += 1
expLine = expected.readline().rstrip()
actLine = actual.readline().rstrip()
## check that the files haven't ended,
# or that they ended at the same time
if expLine == "":
if actLine != "":
f.fail("Tested file ended too late.")
break
if actLine == "":
f.fail("Tested file ended too early.")
break
compareStrings(f, options, expLine, actLine, lineNum)
#print "%3d: %s|%s" % (lineNum, expLine[0:10], actLine[0:10])
f.exit()
################################################################################
if __name__ == '__main__':
parser = OptionParser(usage = "%prog [options] ExpectedFile NewFile")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="Don't print status messages to stdout")
parser.add_option("--check-text",
action="store_true", dest="checkText", default=False,
help="Verify that lines of text match exactly")
parser.add_option("-t", "--tolerance",
action="store", type="float", dest="tol", default=1.e-15,
help="Relative error when comparing doubles")
(options, args) = parser.parse_args()
if len(args) != 2:
print "Usage: numdiff.py EXPECTED ACTUAL"
sys.exit(1)
run(args[0], args[1], options)
其他提示
你应该去 PyUnit, ,现在是标准的一部分lib的名义下 unittest
.它支持你的一切要求。差检查,例如,是完成 assertAlmostEqual()
.
的 ndiff 实用可靠近你在寻找什么:这就像差异,但它将比较文本文件的数字所需的容忍。
我知道我迟到了,但几个月前我写的 nrtest 实用程序以试图使这个工作流程更容易。这听起来像它可能会帮助你。
这里有一个简单的概述。每个测试的定义是通过其输入文件和期望输出的文件。以下执行,输出的文件都存储在一个便携式的基准目录。第二个步骤,随后将这一基准是一个参考基准。最近的一次更新已启用了用户扩展,因此可以定义比较的功能为你定制的数据。
我希望这有所帮助。
不隶属于 StackOverflow