Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to Use SeleniumBase’s sb Fixture in Class-Scoped Setup with Pytest #3390

Answered by mdmintz
smanenti asked this question in Q&A
Discussion options

Hi everyone,

First of all, many thanks for this awesome automation library!

I've done some research, but I failed to find something that would fit our needs:

I’m currently working on a test automation project using SeleniumBase and Pytest, and I’ve encountered a challenge with fixture scopes. SeleniumBase’s sb fixture is limited to function scope, but I need to perform setup tasks at the class scope to avoid repeating the same initialization for every test in the class.

Here’s the scenario:

I want to prepare some shared data or environment setup (using SeleniumBase) once per test class.

Each test in the class should still access the sb fixture individually.

Since the sb fixture is restricted to function scope, I’m struggling to integrate it within a class-scoped setup fixture.

Has anyone faced this limitation before? How did you handle this?
Any ideas, workarounds, or alternative patterns to manage this would be highly appreciated!

Some additional information:

Our automation architecture relies heavily on the sb fixture, which we use throughout our Page and Component objects.

We’ve created a CustomBaseCase class that extends the BaseCase class, and we’ve overridden the sb fixture in a global conftest.py. This custom fixture instantiates the CustomBaseCase and ensures that the setUp() method is called as needed.

We took this approach because we needed to implement various custom methods and features to better fit our testing requirements.

Thanks in advance for your insights!

You must be logged in to vote

You can override the sb class fixture in order to accomplish that. (One of the 25 Syntax Formats)

Eg. SeleniumBase/examples/test_override_sb_fixture.py

That basically lets you do anything that's allowable by pytest, while still getting access to SeleniumBase's BaseCase methods.

Note that overriding the sb fixture means replacing all default configuration in browser_launcher.py, which means special modes that are configured in browser_launcher.py (such as UC Mode, CDP Mode, etc.) won't be there anymore. Depending on what you're doing, that might not be the best option for you.

Another option would be pytest --dist=loadscope to group tests by class. (https://stackoverflow.com/a/56475200/705...

Replies: 1 comment 1 reply

Comment options

You can override the sb class fixture in order to accomplish that. (One of the 25 Syntax Formats)

Eg. SeleniumBase/examples/test_override_sb_fixture.py

That basically lets you do anything that's allowable by pytest, while still getting access to SeleniumBase's BaseCase methods.

Note that overriding the sb fixture means replacing all default configuration in browser_launcher.py, which means special modes that are configured in browser_launcher.py (such as UC Mode, CDP Mode, etc.) won't be there anymore. Depending on what you're doing, that might not be the best option for you.

Another option would be pytest --dist=loadscope to group tests by class. (https://stackoverflow.com/a/56475200/7058266)

If tests are grouped by class (via --dist=loadscope), then you could theoretically put class setUp in the first "test", and tearDown in the last "test". (Tests run alphabetically, so you could have test_aaa be first and test_zzz be last, for example.)

Also useful are the --rs / --reuse-session and --rcs / --reuse-class-session options, which let you reuse the same browser for all tests, or all tests in the same class. (That's another workaround for getting class-scoped tests.)

You must be logged in to vote
1 reply
Comment options

Thanks a lot for your quick and detailed answer, I really appreciate.

We're already using a custom SB fixture + reuse-session, and we are also grouping test execution by class.

I think we will then go with your suggestion with test_aaa and test_zzz (but with pytest-order first and last marker). We'll also overwrite the pytest log_reporter to exclude these tests from the report with maybe a specific marker for these setUp / tearDown tests (as we do not want them to appear in the JUNIT).

Thanks again!

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /