【发布时间】:2019-01-13 13:23:51
【问题描述】:
- 我正在使用 pytest 为 Python 库编写单元测试
- 我需要为测试文件指定一个目录以避免自动测试文件发现,因为有一个很大的子目录结构,包括库中包含“_test”或“test_”的许多文件在名称中,但不适用于 pytest
- 库中的某些文件使用 argparse 来指定命令行选项
- 问题在于将 pytest 的目录指定为命令行参数似乎会干扰使用 argparse 的命令行选项
举个例子,我在根目录下有个文件叫script_with_args.py,如下:
import argparse
def parse_args():
parser = argparse.ArgumentParser(description="description")
parser.add_argument("--a", type=int, default=3)
parser.add_argument("--b", type=int, default=5)
return parser.parse_args()
我在根目录中还有一个名为 tests 的文件夹,其中包含一个名为 test_file.py 的测试文件:
import script_with_args
def test_script_func():
args = script_with_args.parse_args()
assert args.a == 3
如果我从命令行调用python -m pytest,则测试通过。如果我在命令行中用python -m pytest tests指定测试目录,会返回如下错误:
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: C:\Users\Jake\CBAS\pytest-tests, inifile:
plugins: remotedata-0.2.1, openfiles-0.3.0, doctestplus-0.1.3, arraydiff-0.2
collected 1 item
tests\test_file.py F [100%]
================================== FAILURES ===================================
______________________________ test_script_func _______________________________
def test_script_func():
# a = 1
# b = 2
> args = script_with_args.parse_args()
tests\test_file.py:13:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
script_with_args.py:9: in parse_args
return parser.parse_args()
..\..\Anaconda3\lib\argparse.py:1733: in parse_args
self.error(msg % ' '.join(argv))
..\..\Anaconda3\lib\argparse.py:2389: in error
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = ArgumentParser(prog='pytest.py', usage=None, description='description', f
ormatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_h
elp=True)
status = 2, message = 'pytest.py: error: unrecognized arguments: tests\n'
def exit(self, status=0, message=None):
if message:
self._print_message(message, _sys.stderr)
> _sys.exit(status)
E SystemExit: 2
..\..\Anaconda3\lib\argparse.py:2376: SystemExit
---------------------------- Captured stderr call -----------------------------
usage: pytest.py [-h] [--a A] [--b B]
pytest.py: error: unrecognized arguments: tests
========================== 1 failed in 0.19 seconds ===========================
我的问题是,如何在不干扰 argparse 的命令行选项的情况下为 pytest 指定测试文件目录?
【问题讨论】:
-
您的问题不是如何避免在测试会话中传递 CLI args,而是如何为 CLI args 解析逻辑编写适当的测试。您有两种可能性:模拟
sys.argv列表或ArgumentParser.parse_args返回值,或重构您的parse_args函数,使其接受 args 列表。两种方式在链接问题的答案中都有示例。 -
@hoefling 或者stackoverflow.com/questions/18668947/…
标签: python command-line-interface pytest argparse