Skip to content

Commit a811e87

Browse files
committed
Add a class method to run "pytest" if "python" was used instead
1 parent cdd0d56 commit a811e87

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,35 @@ def __initialize_variables(self):
172172
self._chart_series_count = {}
173173
self._tour_steps = {}
174174

175+
@classmethod
176+
def main(self, name, file, *args):
177+
"""Run pytest if file was called with "python".
178+
Usage example:
179+
180+
from seleniumbase import BaseCase
181+
BaseCase.main(__name__, __file__)
182+
183+
class MyTestClass(BaseCase):
184+
def test_example(self):
185+
pass
186+
187+
The run command:
188+
python my_test.py # (Instead of "pytest my_test.py")
189+
190+
This is useful when sharing code with people who may not be aware
191+
that SeleniumBase tests are run with "pytest" instead of "python".
192+
Now, if they accidentally type "python", the tests will still run.
193+
Eg. "python my_test.py" instead of "pytest my_test.py".
194+
"""
195+
if name == "__main__": # Test called with "python"
196+
from pytest import main as pytest_main
197+
all_args = []
198+
for arg in args:
199+
all_args.append(arg)
200+
for arg in sys.argv[1:]:
201+
all_args.append(arg)
202+
pytest_main([file, "-s", *all_args])
203+
175204
def open(self, url):
176205
"""Navigates the current browser window to the specified page."""
177206
self.__check_scope()

0 commit comments

Comments
 (0)