我是python的新手,我正在试图弄清楚如何测试这段代码。
这是我的班级:
#!/usr/bin/python
import argparse
from generator import *
parse = argparse.ArgumentParser(description='XOS Generative Toolchain')
parse.add_argument('--rev', dest='rev', action='store_true',default=False, help='Convert proto to xproto')
parse.add_argument('--target', dest='target', action='store',default=None, help='Output format, corresponding to <output>.yaml file', required=True)
parse.add_argument('--output', dest='output', action='store',default=None, help='Destination dir', required=True)
parse.add_argument('--attic', dest='attic', action='store',default=None, help='The location at which static files are stored')
parse.add_argument('--kvpairs', dest='kv', action='store',default=None, help='Key value pairs to make available to the target')
parse.add_argument('inputs', metavar='<input file>', nargs='+', action='store', help='xproto files to compile')
class XosGen:
@staticmethod
def init(args=None):
if not args:
args = parse.parse_args()
args.quiet = False
# convert output to absolute path
if not os.path.isabs(args.output):
args.output = os.path.abspath(os.getcwd() + '/' + args.output)
if not os.path.isabs(args.target):
args.target = os.path.abspath(os.getcwd() + '/' + args.target)
inputs = []
for fname in args.inputs:
if not os.path.isabs(fname):
inputs.append(os.path.abspath(os.getcwd() + '/' + fname))
else:
inputs.append(fname)
args.inputs = inputs
print args.output
generator = XOSGenerator(args)
generator.generate()
这是测试: 进口单位测试 来自模拟导入补丁 来自xosgen.xosgen导入XosGen
class Args:
pass
class XOSGeneratorTest(unittest.TestCase):
"""
Testing the CLI binding for the XOS Generative Toolchain
"""
def test_generator(self):
"""
[XOS-GenX] The CLI entry point should correctly parse params
"""
args = Args()
args.inputs = ['../test.xproto']
args.target = '../test.xtarget'
args.output = '../out/dir'
expected_args = Args()
expected_args.inputs = ['/Users/teone/Sites/opencord/orchestration/xos/lib/test.xproto']
expected_args.target = '/Users/teone/Sites/opencord/orchestration/xos/lib/test.xtarget'
expected_args.output = '/Users/teone/Sites/opencord/orchestration/xos/lib/out/dir'
with patch("xosgen.xosgen.XOSGenerator") as generator:
XosGen.init(args)
generator.assert_called_with(expected_args)
if __name__ == '__main__':
unittest.main()
我想测试的是,如果传递的参数被正确更改,但我的测试失败了:
AssertionError: Expected call: XOSGenerator(<test_cli.Args instance at 0x10abc29e0>)
Actual call: XOSGenerator(<test_cli.Args instance at 0x10abc2950>)
测试这个的正确方法是什么?
答案 0 :(得分:1)
通过同时调用
args = Args()
和
expected_args = Args()
您将这两个变量分配给两个不同的对象,因此即使它们的属性相同,它们也会使测试用例失败。
要解决此问题,您可能需要专门比较它们的属性。参考call_args,
with patch("xosgen.xosgen.XOSGenerator") as generator:
XosGen.init(args)
actual_args = generator.call_args[1]['args'] # if XOSGenerator takes in a keyword argument
actual_args = generator.call_args[0][0] # otherwise, so comment one of these lines out
assert actual_args.inputs == expected_args.inputs
assert actual_args.target == expected_args.target
assert actual_args.output == expected_args.output