Pytest - getting the value of a fixture parameter
Is there a way to store the value of the parameter provided by the pytest app:
Here is an example conftest.py
# content of conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--parameter", action="store", default="default",
help="configuration file path")
@pytest.fixture
def param(request):
parameter = request.config.getoption("--parameter")
return parameter
Here is an example pytest module:
# content of my_test.py
def test_parameters(param):
assert param == "yes"
OK - everything is working fine, but is there a way to get the value param
outside of the test - for example using some built-in pytest functionpytest.get_fixture_value["parameter"]
EDIT - DETAILED DESCRIPTION WHAT I WANT TO ACHIEVE
I am writing a module that deploys and then provides parameters for tests written to pytest. My idea is that if someone checks this:
class TestApproachI:
@load_params_as_kwargs(parameters_A)
def setup_class(cls, param_1, param_2, ... , param_n):
# code of setup_class
def teardown_class(cls):
# some code
def test_01(self):
# test code
And this someone gives me a config file that explains with what parameters to run his code, I will parse those parameters (in some other script) and I will run his tests with a command pytest --parameters=path_to_serialized_python_tuple test_to_run
where this tuple will contain the provided values ββfor these parameters in the correct order. And I will tell this guy (with tests) to add this decorator to whatever tests he wants to provide me with. This decorator will look like this:
class TestApproachI:
# this path_to_serialized_tuple should be provided by 'pytest --parameters=path_to_serialized_python_tuple test_to_run'
@load_params(path_to_serialized_tuple)
def setup_class(cls, param_1, param_2, ... , param_n):
# code of setup_class
def teardown_class(cls):
# some code
def test_01(self):
# test code
The decorator function should look like this:
def load_params(parameters):
def decorator(func_to_decorate):
@wraps(func_to_decorate)
def wrapper(self):
# deserialize the tuple and decorates replaces the values of test parameters
return func_to_decorate(self, *parameters)
return wrapper
return decorator
source to share
Set parameter
as environment variable os and then use it anywhere in your test viaos.getenv('parameter')
So you can use for example
@pytest.fixture
def param(request):
parameter = request.config.getoption("--parameter")
os.environ["parameter"]=parameter
return parameter
@pytest.mark.usefixtures('param')
def test_parameters(param):
assert os.getenv('parameter') == "yes"
source to share