[TIP] User defined pytest parameter in setup
Brian Okken
variedthoughts at gmail.com
Fri Mar 22 06:54:10 PDT 2019
Fabian,
Use the new `pytestconfig` fixture.
# --------- conftest.py
def pytest_addoption(parser):
parser.addoption("--name", action="store", default="default name")
# ------- test_param.py
import pytest
@pytest.fixture()
def name(pytestconfig):
return pytestconfig.getoption("name")
def test_print_name(name):
print(f"\ncommand line param (name): {name}")
# ---- test run
$ pytest -q -s --name Brian test_param.py
command line param (name): Brian
.
1 passed in 0.01 seconds
# ---
On Fri, Mar 22, 2019 at 4:00 AM Fabian Cenedese <Cenedese at indel.ch> wrote:
> Hello
>
> I want to parametrize a test I have. In the test file are several
> test functions together with a setup/teardown pair. I now want
> to give the setup function a value, preferably via command line
> option. I have added a conftest.py that adds the option (taken
> from the example, with pytest_addoption). In the setup
> function I can get the option with pytest.config.getoption.
> This works. However I get the warning that pytest.config is
> deprecated. How can I solve this using a fixture if I can't
> add request as parameter to the setup function? Or is there
> another way to use a test with different user definable values?
>
> Thanks
>
> bye Fabi
>
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20190322/b7b74005/attachment.html>
More information about the testing-in-python
mailing list