当从命令行运行pytest时,即使我没有告诉它跳过一些参数化的测试,也要不断跳过它们。奇怪的是,在使用vscode中的命令调色板运行时,它不会跳过测试。
我试过单独运行测试和测试文件,并调整命令行选项,但都不起作用。大概,我遗漏了一些微妙的配置。你能帮忙吗?
实例测试
@pytest.mark.parametrize(
"inputs, expec",
helpers.get_samples('inouts/kmp'),
ids=helpers.get_ids('inouts/kmp'))
def test_kmp(capsys, inputs, expec):
"""Sample test
"""
with patch('kmp.sys.stdin', inputs):
kmp.main()
captured = capsys.readouterr()
print(captured.err, file=sys.stderr)
assert captured.out == expec.getvalue()
辅助功能(支持参数化)
INGLOB = '*in*'
OUTGLOB = '*out*'
def _get_globs(path):
"""Collect input/output filename pairs
"""
if not path.endswith("/"):
path = path + "/"
infiles = sorted(glob.glob(path + INGLOB))
outfiles = sorted(glob.glob(path + OUTGLOB))
assert [i.split('.')[0]
for i in infiles] == [o.split('.')[0] for o in outfiles]
return zip(infiles, outfiles)
def get_samples(path):
"""Reads sample inputs/outputs into StringIO memory buffers
"""
files = _get_globs(path)
inputs_outputs = []
for infile, outfile in files:
with open(infile, 'r') as f1:
inputs = StringIO(f1.read())
with open(outfile, 'r') as f2:
outputs = StringIO(f2.read())
inputs_outputs.append(tuple([inputs, outputs]))
return inputs_outputs
def get_ids(path):
"""Returns list of filenames as test ids
"""
return [f for f, _ in _get_globs(path)]
从命令调色板在vscode中运行此项目将生成:
platform darwin -- Python 3.7.2, pytest-4.1.0, py-1.7.0, pluggy-0.8.1
rootdir: ... , inifile:
plugins: cov-2.6.1
collected 79 items
test_1.py ........................................ [ 50%]
test_2.py ................................... [ 94%]
test_3.py .... [100%]
generated xml file:
/var/folders/pn/y4jjr_t531g3x3v0s0snqzf40000gn/T/tmp-...
========================== 79 passed in 0.55 seconds ===========================
但是从命令行运行相同的测试会产生:
=============================== test session starts ===============================
platform darwin -- Python 3.7.2, pytest-4.1.0, py-1.7.0, pluggy-0.8.1
rootdir: ... , inifile:
plugins: cov-2.6.1
collected 59 items
test_1.py ssss...................s..... [ 49%]
test_2.py s.......................ss.s [ 96%]
test_3.py s. [100%]
====================== 49 passed, 10 skipped in 0.42 seconds ======================
如何让pytest收集并运行全部76个项目?我没有在vscode中使用任何特殊选项。我不明白为什么Pytest没有被告知跳过测试。
谢谢!