Skip to content

Commit 1a2a910

Browse files
committed
Improve non-pytest script processing during pytest collection
1 parent e6a4477 commit 1a2a910

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

seleniumbase/plugins/driver_manager.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,39 @@ def Driver(
127127
wire=None, # Shortcut / Duplicate of "use_wire".
128128
pls=None, # Shortcut / Duplicate of "page_load_strategy".
129129
):
130+
from seleniumbase import config as sb_config
130131
from seleniumbase.fixtures import constants
131132
from seleniumbase.fixtures import shared_utils
132133

133134
sys_argv = sys.argv
134135
arg_join = " ".join(sys_argv)
136+
existing_runner = False
137+
collect_only = ("--co" in sys_argv or "--collect-only" in sys_argv)
138+
all_scripts = (hasattr(sb_config, "all_scripts") and sb_config.all_scripts)
139+
if (
140+
(hasattr(sb_config, "is_behave") and sb_config.is_behave)
141+
or (hasattr(sb_config, "is_pytest") and sb_config.is_pytest)
142+
or (hasattr(sb_config, "is_nosetest") and sb_config.is_nosetest)
143+
):
144+
existing_runner = True
145+
if (
146+
existing_runner
147+
and not hasattr(sb_config, "_context_of_runner")
148+
):
149+
if hasattr(sb_config, "is_pytest") and sb_config.is_pytest:
150+
import pytest
151+
msg = "Skipping `Driver()` script. (Use `python`, not `pytest`)"
152+
if not collect_only and not all_scripts:
153+
print("\n *** %s" % msg)
154+
if collect_only or not all_scripts:
155+
pytest.skip(allow_module_level=True)
156+
elif hasattr(sb_config, "is_nosetest") and sb_config.is_nosetest:
157+
raise Exception(
158+
"\n A Driver() script was triggered by nosetest collection!"
159+
'\n (Prevent that by using: ``if __name__ == "__main__":``)'
160+
)
161+
elif existing_runner:
162+
sb_config._context_of_runner = True
135163
browser_changes = 0
136164
browser_set = None
137165
browser_text = None

seleniumbase/plugins/pytest_plugin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,16 @@ def pytest_addoption(parser):
759759
help="""This is used by the BaseCase class to tell apart
760760
pytest runs from nosetest runs. (Automatic)""",
761761
)
762+
parser.addoption(
763+
"--all-scripts",
764+
"--all_scripts",
765+
action="store_true",
766+
dest="all_scripts",
767+
default=False,
768+
help="""Use this to run `SB()`, `DriverContext()` and
769+
`Driver()` scripts that are discovered during
770+
the pytest collection phase.""",
771+
)
762772
parser.addoption(
763773
"--time_limit",
764774
"--time-limit",
@@ -1525,6 +1535,7 @@ def pytest_configure(config):
15251535
settings.ARCHIVE_EXISTING_DOWNLOADS = True
15261536
if config.getoption("skip_js_waits"):
15271537
settings.SKIP_JS_WAITS = True
1538+
sb_config.all_scripts = config.getoption("all_scripts")
15281539
sb_config._time_limit = config.getoption("time_limit")
15291540
sb_config.time_limit = config.getoption("time_limit")
15301541
sb_config.slow_mode = config.getoption("slow_mode")

seleniumbase/plugins/sb_manager.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ def SB(
136136
arg_join = " ".join(sys_argv)
137137
archive_logs = False
138138
existing_runner = False
139+
collect_only = ("--co" in sys_argv or "--collect-only" in sys_argv)
140+
all_scripts = (hasattr(sb_config, "all_scripts") and sb_config.all_scripts)
139141
do_log_folder_setup = False # The first "test=True" run does it
140142
if (
141143
(hasattr(sb_config, "is_behave") and sb_config.is_behave)
@@ -146,21 +148,21 @@ def SB(
146148
test = False # Already using a test runner. Skip extra test steps.
147149
elif test is None and "--test" in sys_argv:
148150
test = True
149-
if (
150-
existing_runner
151-
and not hasattr(sb_config, "_context_of_runner")
152-
):
153-
sb_config._context_of_runner = True
151+
if existing_runner and not hasattr(sb_config, "_context_of_runner"):
154152
if hasattr(sb_config, "is_pytest") and sb_config.is_pytest:
155-
print(
156-
"\n SB Manager script was triggered by pytest collection!"
157-
'\n (Prevent that by using: `if __name__ == "__main__":`)'
158-
)
153+
import pytest
154+
msg = "Skipping `SB()` script. (Use `python`, not `pytest`)"
155+
if not collect_only and not all_scripts:
156+
print("\n *** %s" % msg)
157+
if collect_only or not all_scripts:
158+
pytest.skip(allow_module_level=True)
159159
elif hasattr(sb_config, "is_nosetest") and sb_config.is_nosetest:
160160
raise Exception(
161161
"\n SB Manager script was triggered by nosetest collection!"
162162
'\n (Prevent that by using: ``if __name__ == "__main__":``)'
163163
)
164+
elif existing_runner:
165+
sb_config._context_of_runner = True
164166
if (
165167
not existing_runner
166168
and not hasattr(sb_config, "_has_older_context")
@@ -959,8 +961,6 @@ def SB(
959961
sb_config = sb_config_backup
960962
if test:
961963
sb_config._has_older_context = True
962-
if existing_runner:
963-
sb_config._context_of_runner = True
964964
if test_name:
965965
result = "passed"
966966
if test and not test_passed:

0 commit comments

Comments
 (0)