gpt4 book ai didi

带有 argparse 的 Python 单元测试

转载 作者:行者123 更新时间:2023-12-03 14:39:23 26 4
gpt4 key购买 nike

我正在尝试将 unittest 与使用 argparse 模块并遇到一些困难的程序一起使用。我引用了this helpful post作为一个起点,但我显然仍然缺少一些东西。

这是基本程序:

#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()

运行这个:
python arg_test.py --arg1 asdf --arg2 qwer

导致预期输出:
the args are: Namespace(arg1='asdf', arg2='qwer')
the value of arg1 is : asdf
the value of arg2 is : qwer

现在这是一个简单的单元测试程序:
#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()

运行这个:
python -m unittest -v test/test_arg_test.py --arg1 asdf --arg2 qwer

结果如下:
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

有人可以为我指出如何运行这些测试的正确方向吗?

谢谢。

更新#1

这是基于以下有用建议的更新单元测试程序,但仍然缺少一些东西。
#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()

运行这个:
python -m unittest -v test/test_arg_test.py

结果如下:
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)

更新#2
#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()

运行这个:
python -m unittest -v test/test_arg_test.py

结果如下:
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)

最佳答案

使用 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)

关于带有 argparse 的 Python 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49838007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com