Skip to content

Commit 2ce0391

Browse files
committed
Skip unnecessary pytest plugin steps when using --collect-only / --co
1 parent bdd4a96 commit 2ce0391

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

seleniumbase/plugins/pytest_plugin.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
is_windows = False
1414
if sys.platform in ["win32", "win64", "x64"]:
1515
is_windows = True
16+
sys_argv = sys.argv
1617
pytest_plugins = ["pytester"] # Adds the "testdir" fixture
1718

1819

@@ -1028,8 +1029,7 @@ def pytest_addoption(parser):
10281029
folder, such as when running tests in GitHub Actions.""",
10291030
)
10301031

1031-
sys_argv = sys.argv
1032-
arg_join = " ".join(sys.argv)
1032+
arg_join = " ".join(sys_argv)
10331033
sb_config._browser_shortcut = None
10341034

10351035
# SeleniumBase does not support pytest-timeout due to hanging browsers.
@@ -1314,11 +1314,11 @@ def pytest_configure(config):
13141314
sb_config._dash_final_summary = None # Dash status to add to html report
13151315
sb_config._html_report_name = None # The name of the pytest html report
13161316

1317-
arg_join = " ".join(sys.argv)
1317+
arg_join = " ".join(sys_argv)
13181318
if (
1319-
"-n" in sys.argv
1319+
"-n" in sys_argv
13201320
or " -n=" in arg_join
1321-
or "-c" in sys.argv
1321+
or "-c" in sys_argv
13221322
or (
13231323
sys.version_info[0] >= 3
13241324
and "addopts" in config.inicfg.keys()
@@ -1330,7 +1330,7 @@ def pytest_configure(config):
13301330
):
13311331
sb_config._multithreaded = True
13321332
if (
1333-
"--html" in sys.argv
1333+
"--html" in sys_argv
13341334
or " --html=" in arg_join
13351335
or (
13361336
sys.version_info[0] >= 3
@@ -1398,13 +1398,14 @@ def pytest_configure(config):
13981398
if sb_config.dash_title:
13991399
constants.Dashboard.TITLE = sb_config.dash_title.replace("_", " ")
14001400

1401-
from seleniumbase.core import log_helper
1402-
from seleniumbase.core import download_helper
1403-
from seleniumbase.core import proxy_helper
1401+
if "--co" not in sys_argv and "--collect-only" not in sys_argv:
1402+
from seleniumbase.core import log_helper
1403+
from seleniumbase.core import download_helper
1404+
from seleniumbase.core import proxy_helper
14041405

1405-
log_helper.log_folder_setup(sb_config.log_path, sb_config.archive_logs)
1406-
download_helper.reset_downloads_folder()
1407-
proxy_helper.remove_proxy_zip_if_present()
1406+
log_helper.log_folder_setup(sb_config.log_path, sb_config.archive_logs)
1407+
download_helper.reset_downloads_folder()
1408+
proxy_helper.remove_proxy_zip_if_present()
14081409

14091410

14101411
def pytest_sessionstart(session):
@@ -1470,6 +1471,8 @@ def _create_dashboard_assets_():
14701471

14711472

14721473
def pytest_itemcollected(item):
1474+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1475+
return
14731476
if sb_config.dashboard:
14741477
sb_config.item_count += 1
14751478
test_id, display_id = _get_test_ids_(item)
@@ -1480,6 +1483,8 @@ def pytest_itemcollected(item):
14801483

14811484

14821485
def pytest_deselected(items):
1486+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1487+
return
14831488
if sb_config.dashboard:
14841489
sb_config.item_count -= len(items)
14851490
for item in items:
@@ -1493,6 +1498,8 @@ def pytest_collection_finish(session):
14931498
Print the dashboard path if at least one test runs.
14941499
https://docs.pytest.org/en/stable/reference.html
14951500
"""
1501+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1502+
return
14961503
if sb_config.dashboard and len(session.items) > 0:
14971504
_create_dashboard_assets_()
14981505
# Output Dashboard info to the console
@@ -1517,6 +1524,8 @@ def pytest_collection_finish(session):
15171524

15181525
def pytest_runtest_setup(item):
15191526
"""This runs before every test with pytest."""
1527+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1528+
return
15201529
if sb_config.dashboard:
15211530
sb_config._sbase_detected = False
15221531
test_id, display_id = _get_test_ids_(item)
@@ -1528,6 +1537,8 @@ def pytest_runtest_teardown(item):
15281537
"""This runs after every test with pytest.
15291538
Make sure that webdriver and headless displays have exited.
15301539
(Has zero effect on tests using --reuse-session / --rs)"""
1540+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1541+
return
15311542
try:
15321543
if hasattr(item, "_testcase") or hasattr(sb_config, "_sb_pdb_driver"):
15331544
if hasattr(item, "_testcase"):
@@ -1536,7 +1547,7 @@ def pytest_runtest_teardown(item):
15361547
if (
15371548
hasattr(self, "driver")
15381549
and self.driver
1539-
and "--pdb" not in sys.argv
1550+
and "--pdb" not in sys_argv
15401551
):
15411552
if not is_windows or self.driver.service.process:
15421553
self.driver.quit()
@@ -1557,11 +1568,11 @@ def pytest_runtest_teardown(item):
15571568
pass
15581569
try:
15591570
if hasattr(self, "xvfb") and self.xvfb:
1560-
if self.headless_active and "--pdb" not in sys.argv:
1571+
if self.headless_active and "--pdb" not in sys_argv:
15611572
if hasattr(self, "display") and self.display:
15621573
self.display.stop()
15631574
elif hasattr(self, "headless") and self.headless:
1564-
if self.headless_active and "--pdb" not in sys.argv:
1575+
if self.headless_active and "--pdb" not in sys_argv:
15651576
if hasattr(self, "display") and self.display:
15661577
self.display.stop()
15671578
except Exception:
@@ -1577,6 +1588,8 @@ def pytest_sessionfinish(session):
15771588

15781589

15791590
def pytest_terminal_summary(terminalreporter):
1591+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1592+
return
15801593
latest_logs_dir = os.getcwd() + "/latest_logs/"
15811594
if sb_config._multithreaded:
15821595
if os.path.exists(latest_logs_dir) and os.listdir(latest_logs_dir):
@@ -1657,7 +1670,7 @@ def _perform_pytest_unconfigure_():
16571670
the_html_d = None
16581671
with open(dashboard_path, "r", encoding="utf-8") as f:
16591672
the_html_d = f.read()
1660-
if sb_config._multithreaded and "-c" in sys.argv:
1673+
if sb_config._multithreaded and "-c" in sys_argv:
16611674
# Threads have "-c" in sys.argv, except for the last
16621675
raise Exception('Break out of "try" block.')
16631676
if sb_config._multithreaded:
@@ -1748,6 +1761,8 @@ def _perform_pytest_unconfigure_():
17481761

17491762
def pytest_unconfigure(config):
17501763
"""This runs after all tests have completed with pytest."""
1764+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1765+
return
17511766
if hasattr(sb_config, "_multithreaded") and sb_config._multithreaded:
17521767
import fasteners
17531768

@@ -1830,6 +1845,8 @@ def base_method(self):
18301845

18311846
@pytest.mark.hookwrapper
18321847
def pytest_runtest_makereport(item, call):
1848+
if "--co" in sys_argv or "--collect-only" in sys_argv:
1849+
return
18331850
pytest_html = item.config.pluginmanager.getplugin("html")
18341851
outcome = yield
18351852
report = outcome.get_result()

0 commit comments

Comments
 (0)