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

cur*_*w77 5 python pytest

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()
Run Code Online (Sandbox Code Playgroud)

辅助函数(支持参数化)

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)]
Run Code Online (Sandbox Code Playgroud)

从命令面板在 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 ===========================
Run Code Online (Sandbox Code Playgroud)

但是从命令行运行相同的测试会产生:

=============================== 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 ======================
Run Code Online (Sandbox Code Playgroud)

如何让 pytest 收集和运行全部 76 个项目?我没有在 VSCode 中使用任何特殊选项。我不明白为什么 pytest 在没有被告知的情况下跳过测试。

谢谢!

Ant*_*ile 4

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

最简单的例子:

@pytest.mark.parametrize('a', ())
def test(a): pass
Run Code Online (Sandbox Code Playgroud)

和输出

$ pytest -v t.py 

...

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

=========================== 1 skipped in 0.02 seconds ===========================
Run Code Online (Sandbox Code Playgroud)

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