pytest pytest_generate_tests() 中的问题

Issue in pytest_generate_tests() in pytest

"conftest.py"

def pytest_addoption(parser):
    parser.addoption("--stringinput", action= "append", default = ["defaultstring"], help = "Input String")

def pytest_generate_tests(metafunc):
    if "stringinput" in metafunc.fixturenames:
         metafunc.parametrize("stringinput",metafunc.config.getoption("stringinput"))

当我们通过将命令行选项“stringinput”作为“pytest pytest_generate_test_example.py --stringinput="hello" 传递来执行此代码时,它还会执行输出中的默认值。 输出的片段如下:--

pytest_generate_test_example.py::test_valid_string[默认字符串] 通过pytest_generate_test_example.py::test_valid_string[你好] 通过

这是 argparse 的 "problem" 与 pytest 无关。查看测试脚本:

#! /usr/bin/env python

import argparse

parser = argparse.ArgumentParser(description='Import')
parser.add_argument('test', action='append', default=['foo'])
args = parser.parse_args('bar'.split())

print args.test

输出['foo', 'bar']。 IE。 action='append' 附加到 default 而不先清除它。

因此,为避免此问题,请从一个空的 default 列表开始,如果参数为空(没有 --stringinput 命令行参数),则使用您的 defaultstring