-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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"]
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks a lot!
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1