Beispiel #1
0
def test_make_hook_recorder(pytester: Pytester) -> None:
    item = pytester.getitem("def test_func(): pass")
    recorder = pytester.make_hook_recorder(item.config.pluginmanager)
    assert not recorder.getfailures()

    # (The silly condition is to fool mypy that the code below this is reachable)
    if 1 + 1 == 2:
        pytest.xfail("internal reportrecorder tests need refactoring")

    class rep:
        excinfo = None
        passed = False
        failed = True
        skipped = False
        when = "call"

    recorder.hook.pytest_runtest_logreport(
        report=rep)  # type: ignore[attr-defined]
    failures = recorder.getfailures()
    assert failures == [rep]  # type: ignore[comparison-overlap]
    failures = recorder.getfailures()
    assert failures == [rep]  # type: ignore[comparison-overlap]

    class rep2:
        excinfo = None
        passed = False
        failed = False
        skipped = True
        when = "call"

    rep2.passed = False
    rep2.skipped = True
    recorder.hook.pytest_runtest_logreport(
        report=rep2)  # type: ignore[attr-defined]

    modcol = pytester.getmodulecol("")
    rep3 = modcol.config.hook.pytest_make_collect_report(collector=modcol)
    rep3.passed = False
    rep3.failed = True
    rep3.skipped = False
    recorder.hook.pytest_collectreport(
        report=rep3)  # type: ignore[attr-defined]

    passed, skipped, failed = recorder.listoutcomes()
    assert not passed and skipped and failed

    numpassed, numskipped, numfailed = recorder.countoutcomes()
    assert numpassed == 0
    assert numskipped == 1
    assert numfailed == 1
    assert len(recorder.getfailedcollections()) == 1

    recorder.unregister()  # type: ignore[attr-defined]
    recorder.clear()
    recorder.hook.pytest_runtest_logreport(
        report=rep3)  # type: ignore[attr-defined]
    pytest.raises(ValueError, recorder.getfailures)
    def test_consider_module_import_module(self, pytester: Pytester,
                                           _config_for_test: Config) -> None:
        pytestpm = _config_for_test.pluginmanager
        mod = types.ModuleType("x")
        mod.__dict__["pytest_plugins"] = "pytest_a"
        aplugin = pytester.makepyfile(pytest_a="#")
        reprec = pytester.make_hook_recorder(pytestpm)
        pytester.syspathinsert(aplugin.parent)
        pytestpm.consider_module(mod)
        call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name)
        assert call.plugin.__name__ == "pytest_a"

        # check that it is not registered twice
        pytestpm.consider_module(mod)
        values = reprec.getcalls("pytest_plugin_registered")
        assert len(values) == 1