gpt4 book ai didi

python - 如何强制 pytest 运行每个测试(停止跳过未标记为跳过的测试)?

转载 作者:行者123 更新时间:2023-12-03 23:53:13 49 4
gpt4 key购买 nike

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 在没有被告知的情况下跳过测试。

谢谢!

最佳答案

pytest 将自动“跳过”参数化但没有条目的测试。

最简单的例子:

@pytest.mark.parametrize('a', ())
def test(a): pass

和输出
$ pytest -v t.py 

...

t.py::test[a0] SKIPPED [100%]

=========================== 1 skipped in 0.02 seconds ===========================

您在这里的问题可能是两个执行环境,当前工作目录或某些这样的环境导致您的数据收集在终端中返回零结果,但从 vscode 运行时返回实际结果。我会检查您当前的工作目录和您首先激活的 virtualenv,然后从那里进行调试。甚至可以在 get_samples 里面放一个断点/ get_ids .

关于python - 如何强制 pytest 运行每个测试(停止跳过未标记为跳过的测试)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54275949/

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