Skip to content

Commit

Permalink
plugin: do not run pytest_setup and pytest_teardown with --colect-only
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrezina committed Mar 14, 2024
1 parent 9ed2149 commit cb3adf3
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pytest_mh/_private/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def __init__(self, pytest_config: pytest.Config) -> None:
if self.mh_collect_logs is None:
self.mh_collect_logs = self.mh_collect_artifacts

# pytest options
self.pytest_opt_collect_only: bool = pytest_config.getoption("collectonly")

@classmethod
def GetLogger(cls) -> logging.Logger:
"""
Expand Down Expand Up @@ -161,7 +164,8 @@ def pytest_sessionfinish(self, session: pytest.Session, exitstatus: int | pytest
return

# Run pytest_teardown on all hosts required by selected tests
self._teardown_hosts(self.required_hosts)
if not self.pytest_opt_collect_only:
self._teardown_hosts(self.required_hosts)

@pytest.hookimpl(hookwrapper=True)
def pytest_make_collect_report(self, collector: pytest.Collector) -> Generator[None, pytest.CollectReport, None]:
Expand Down Expand Up @@ -265,8 +269,8 @@ def pytest_collection_modifyitems(self, config: pytest.Config, items: list[pytes
# Sort required host by name to provide deterministic runs
self.required_hosts = sorted(required_hosts_set, key=lambda x: x.hostname)

@pytest.hookimpl(tryfirst=True)
def pytest_collection_finish(self, session: pytest.Session) -> None:
@pytest.hookimpl(hookwrapper=True)
def pytest_collection_finish(self, session: pytest.Session) -> Generator:
# Log required hosts
self.logger.info("")
self.logger.info("")
Expand All @@ -275,6 +279,12 @@ def pytest_collection_finish(self, session: pytest.Session) -> None:
self.logger.info(f" {host.role}: {host.hostname}")
self.logger.info("")

yield

# Run pytest_setup on all hosts required by selected tests
if not self.pytest_opt_collect_only:
self._setup_hosts(self.required_hosts)

@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(self, item: pytest.Item) -> None:
"""
Expand All @@ -295,9 +305,6 @@ def pytest_runtest_setup(self, item: pytest.Item) -> None:
return
mark: TopologyMark = data.topology_mark

# Run pytest_setup on all hosts required by selected tests
self._setup_hosts(self.required_hosts)

# Execute per-topology setup if topology is switched.
if self._topology_switch(None, item):
self.current_topology = mark.name
Expand Down

0 comments on commit cb3adf3

Please sign in to comment.