0

my test_sample.py is like:

import pytest

a,b = myclass('cmdopts').get_spec()

def gen_args(a, b):
    for i in a:
        for j in b:
            yield (i,j)
@pytest.mark.parametrize('a,b', gen_args(a,b))
def test_c1(a, b):
    assert a == b

my question is how can I pass cmdopts to the test script, not to the test_c1 function?

lufy
  • 33
  • 5

1 Answers1

0

pytest.mark.parametrize is not intended to be used in this way.

with pytest_addoption of pytest you can inject yours a, b parameters using metafunc.parametrize

using this technique allow you to set also parameters in environment variables.

conftest.py

def pytest_addoption(parser):
    parser.addoption("--a", action="store")
    parser.addoption("--b", action="store")


def pytest_generate_tests(metafunc):
    for par_name, par_value in (
            ('a',metafunc.config.option.a),
            ('b',metafunc.config.option.b)
        ):
        if par_name in metafunc.fixturenames and par_value:
            metafunc.parametrize(par_name, [par_value])

test_sample.py

def test_c1(a, b):
    assert a == b

CLI

$ pytest test_sample.py --a 42 --b 42

collected 1 item                                                                                                                                                                            

test_sample.py .                                                                                                                                                                      [100%]

= 1 passed in 0.01s =


$ pytest test_sample.py --a 42 --b 24

collected 1 item                                                                                                                                                                            

test_sample.py F                                                                                                                                                                      [100%]

= FAILURES =
_ test_c1[42-24] _

a = '42', b = '24'

    def test_c1(a, b):
>       assert a == b
E       AssertionError: assert '42' == '24'
E         - 24
E         + 42

test_sample.py:2: AssertionError
================================================================================== short test summary info ==================================================================================
FAILED test_sample.py::test_c1[42-24] - AssertionError: assert '42' == '24'
===================================================================================== 1 failed in 0.07s

Something similar here: How to pass arguments in pytest by command line

Glauco
  • 1,385
  • 2
  • 10
  • 20
  • Thanks so much. But I want to pass `cmdopts` from command line, not `a` and `b`. Is this possible? – lufy Dec 29 '21 at 09:20
  • I think I should pass `cmdopts` by environment variables instead of command line arguments – lufy Dec 29 '21 at 09:38
  • you can pass cmdopt and then use a factory to get the right value for a,b or use an env variable – Glauco Dec 29 '21 at 09:49