File: plugin.py

package info (click to toggle)
pytest-relaxed 2.0.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212 kB
  • sloc: python: 960; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 2,234 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pytest

from .classes import SpecModule
from .reporter import RelaxedReporter

# NOTE: fixtures must be present in the module listed under our setup.py's
# pytest11 entry_points value (i.e., this one.) Just being in the import path
# (e.g. package __init__.py) was not sufficient!
from .fixtures import environ  # noqa


def pytest_ignore_collect(collection_path, config):
    # Ignore files and/or directories marked as private via Python convention.
    return collection_path.name.startswith("_")


# We need to use collect_file, not pycollect_makemodule, as otherwise users
# _must_ specify a config blob to use us, vs that being optional.
# TODO: otoh, I personally use it all the time and we "strongly recommend it"
# so maybe find a way to make that config bit default somehow (update
# docs/changelog appropriately), and then switch  hooks?
def pytest_collect_file(file_path, parent):
    # Modify file selection to choose all .py files besides conftest.py.
    # (Skipping underscored names is handled up in pytest_ignore_collect, which
    # applies to directories too.)
    if (
        file_path.suffix != ".py"
        or file_path.name == "conftest.py"
        # Also skip anything prefixed with test_; pytest's own native
        # collection will get that stuff, and we don't _want_ to try modifying
        # such files anyways.
        or file_path.name.startswith("test_")
    ):
        return
    # Then use our custom module class which performs modified
    # function/class selection as well as class recursion
    return SpecModule.from_parent(parent=parent, path=file_path)


@pytest.mark.trylast  # So we can be sure builtin terminalreporter exists
def pytest_configure(config):
    # TODO: we _may_ sometime want to do the isatty/slaveinput/etc checks that
    # pytest-sugar does?
    builtin = config.pluginmanager.getplugin("terminalreporter")
    # Pass the configured, instantiated builtin terminal reporter to our
    # instance so it can refer to e.g. the builtin reporter's configuration
    ours = RelaxedReporter(builtin)
    # Unregister the builtin first so only our output appears
    config.pluginmanager.unregister(builtin)
    config.pluginmanager.register(ours, "terminalreporter")