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

时间:2019-01-20 11:24:37

标签: 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()

Helper函数(支持参数化)

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跳过测试而不被告知这样做。

谢谢!

1 个答案:

答案 0 :(得分:1)

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内放置一个断点。