如何获得收集的测试计数?

时间:2019-07-08 23:21:10

标签: python pytest

我正在尝试计算pytest调用时收集的测试总数。 我想在我的conftest.py中的某个地方使用该数字。

我已经按照此处给出的答案进行了How to get the total number of tests passed, failed and skipped from pytest 但是它使用_pytest.terminal.TerminalReporter,以后可能会更改。

有什么办法,我可以计算pytest使用一些request固定装置等收集的测试数量吗?

3 个答案:

答案 0 :(得分:1)

如果您只需要收集测试量,那么使用终端报告程序确实是一个过高的选择。使用request.session.items列表:

def test_amount(request):
    amount = len(request.session.items)
    assert amount > 0

答案 1 :(得分:0)

这很粗糙,但是您可以尝试解析pytest --collect-only的输出,该输出在标准输出上返回为测试而收集的模块/函数(但不运行测试)。您可以解析该输出以统计收集的测试模块/功能/等。

答案 2 :(得分:0)

tests_count = 0

def pytest_runtestloop(session):
    """
    This function gets us details about the tests that are collected before running.
    """
    global tests_count
    tests_count = session.testscollected

tests_count = 0

def pytest_runtestloop(session):
    """
    This function gets us details about the tests that are collected before running.
    """
    global tests_count
    tests_count = len(session.items)

pytest 版本:6.2.2