use*_*332 6 python unit-testing argparse python-3.x
我正在尝试将 unittest 与使用 argparse 模块并遇到一些困难的程序一起使用。我参考了这篇有用的帖子作为起点,但显然我仍然遗漏了一些东西。
这是基本程序:
#arg_test.py
import sys
import argparse
class Thingy:
def __init__(self, name):
self.name = name
def parse_args(args):
parser = argparse.ArgumentParser(description='description here')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
parser.add_argument('-a', '--arg1', required=True, help='this is for arg1')
parser.add_argument('-b', '--arg2', required=True, help='this is for arg2')
return parser.parse_args()
def main():
parser = Thingy.parse_args(sys.argv[1:])
print('the args are: {}'.format(parser))
if parser.arg1:
print('the value of arg1 is : {}'.format(parser.arg1))
if parser.arg2:
print('the value of arg2 is : {}'.format(parser.arg2))
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
运行这个:
python arg_test.py --arg1 asdf --arg2 qwer
Run Code Online (Sandbox Code Playgroud)
预期输出结果为:
the args are: Namespace(arg1='asdf', arg2='qwer')
the value of arg1 is : asdf
the value of arg2 is : qwer
Run Code Online (Sandbox Code Playgroud)
现在这是一个简单的单元测试程序:
#test/test_arg_test.py
import unittest
from arg_test import Thingy
def test_parser(self):
parser = Thingy.parse_args(['--arg1'])
self.assertTrue(parser.arg1,'asdf')
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
运行这个:
python -m unittest -v test/test_arg_test.py --arg1 asdf --arg2 qwer
Run Code Online (Sandbox Code Playgroud)
结果如下:
python -m unittest -v test/test_arg_test.py --arg1 asdf --arg2 qwer
usage: python -m unittest [-h] [-v] [-q] [--locals] [-f] [-c] [-b]
[tests [tests ...]]
python -m unittest: error: unrecognized arguments: --arg1 asdf --arg2 qwer
Run Code Online (Sandbox Code Playgroud)
有人可以指出我如何运行这些测试的正确方向吗?
谢谢。
这是基于以下有用建议的更新单元测试程序,但仍然缺少某些内容。
#import unittest
from arg_test import Thingy
class TestThingys(unittest.TestCase):
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_parser(self):
argv1 = ['--arg1', 'asdf', '--arg2', 'qwer']
parser = Thingy().parse_args(argv1)
self.assertTrue(parser.arg1,'asdf')
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
运行这个:
python -m unittest -v test/test_arg_test.py
Run Code Online (Sandbox Code Playgroud)
结果如下:
test_isupper (test.test_arg_test.TestThingys) ... ok
test_parser (test.test_arg_test.TestThingys) ... ERROR
======================================================================
ERROR: test_parser (test.test_arg_test.TestThingys)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/username/scripts/python/arg_test/test/test_arg_test.py", line 12, in test_parser
parser = Thingy().parse_args(argv1)
TypeError: __init__() missing 1 required positional argument: 'name'
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (errors=1)
Run Code Online (Sandbox Code Playgroud)
#test/test_arg_test.py
import unittest
from arg_test import Thingy
class TestThingys(unittest.TestCase):
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_parser(self):
argv1 = ['--arg1', 'asdf', '--arg2', 'qwer']
parser = Thingy('name').parse_args(argv1)
self.assertEquals(parser.arg1,'asdf')
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
运行这个:
python -m unittest -v test/test_arg_test.py
Run Code Online (Sandbox Code Playgroud)
结果如下:
test_isupper (test.test_arg_test.TestThingys) ... ok
test_parser (test.test_arg_test.TestThingys) ... ERROR
======================================================================
ERROR: test_parser (test.test_arg_test.TestThingys)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/username/scripts/python/arg_test/test/test_arg_test.py", line 12, in test_parser
parser = Thingy('name').parse_args(argv1)
TypeError: parse_args() takes 1 positional argument but 2 were given
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (errors=1)
Run Code Online (Sandbox Code Playgroud)
argv
在里面使用class TestThingy
def test_parser(self):
argv1 = ['--arg1', 'asdf', '--arg2', 'qwer']
parser = Thingy('name').parse_args(argv1)
self.assertEquals(parser.arg1,'asdf')
argv2 = ['--trigger_exception`, 'asdf`]
with self.assertRaise(Exception):
parser = Thingy('name').parse_args(argv2)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4159 次 |
最近记录: |