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

BaseCase inheritance and pytest addoptions. #1383

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

Hi folks!

In my tests I'm using classic Page Object Model with BaseCase inheritance. I want to add additional arguments to test launch by using fixtures and pytest_addoption. For instance for changing base url:

conftest.py :

import pytest
def pytest_addoption(parser):
 parser.addoption("--env-url", action="store")
@pytest.fixture
def env_url(request):
 if request.config.getoption("--env-url") == 'dev':
 return "https://dev.url.com/"
 elif request.config.getoption("--env-url") == 'stage':
 return "https://stage.url.com/"

and test_email_authorization.py :

import allure
import pytest
from AccountPage import AccountPage
from BaseTestCase import BaseTestCase
from MainPage import MainPage
class Test(BaseTestCase):
 def test_email_authorization(self, env_url):
 """Тest"""
 with allure.step('Open Home Page'):
 self.open(env_url)

Such test fires error when running:

 def _callTestMethod(self, method):
> method()
E TypeError: test_email_authorization() missing 1 required positional argument: 'env_url'

and I guess that the issue is in using BaseCase inheritance syntax.

I have found workaround with overriding setUp() method of BaseCase and using seleniumbase default argument --data, but I wonder if there is a way to add you custom options to tests when using BaseCase inheritace syntax?

You must be logged in to vote

Hello,
When using pytest fixtures with SeleniumBase, you'll want to use one of the syntax formats that uses the sb fixture instead of BaseCase inheritance, because pytest fixtures don't work with tests that inherit unittest.TestCase. You can use the "The classic Page Object Model with the sb pytest fixture" for this.

Or, you can use the existing --env=ENV option to pass the ENV to the tests, where you can put your if statement in there. Eg. --env=staging, --env=qa, --env=test, --env=production, --env=develop. Then use self.env to access that environment in your tests. You have to use one of the predefined values: (choose from 'qa', 'staging', 'develop', 'production', 'master', 'remote', '...

Replies: 1 comment 1 reply

Comment options

Hello,
When using pytest fixtures with SeleniumBase, you'll want to use one of the syntax formats that uses the sb fixture instead of BaseCase inheritance, because pytest fixtures don't work with tests that inherit unittest.TestCase. You can use the "The classic Page Object Model with the sb pytest fixture" for this.

Or, you can use the existing --env=ENV option to pass the ENV to the tests, where you can put your if statement in there. Eg. --env=staging, --env=qa, --env=test, --env=production, --env=develop. Then use self.env to access that environment in your tests. You have to use one of the predefined values: (choose from 'qa', 'staging', 'develop', 'production', 'master', 'remote', 'local', 'alpha', 'beta', 'main', 'test') The reason for this is to prevent misspellings or alternate spellings from having a negative impact or not taking effect.

As you discovered, you can also pass additional options using --data=STRING (or --var1=STRING, --var2=STRING, --var3=STRING, or --variables=DICT)
Example: --variables='{"special":123}'
Then in the test, access that with self.variables["special"]

You must be logged in to vote
1 reply
Comment options

Thanks a lot!

Answer selected by oggeche
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 によって変換されたページ (->オリジナル) /