this part is testing demo fixture & params
1 2 3 4 5 6 7 8 9
| import pytest @pytest.fixture(scope='function', params=[1,2,3]) # multiple test cases def fixture_param(request): return request.param def test_not_2(mock_data_params): print('test_data: %s' % mock_data_params) assert isinstance(mock_data_params, int)
|
In test.py
:
1 2 3 4 5 6 7 8 9
| @pytest.fixture() def controller(test_mode): if test_mode is None: mode = config_dict["sql"] else: mode = config_dict[test_mode] print(mode) print(os.getcwd()) return TestController(mode)
|
In class TestController
:
1 2 3 4
| class TestController(): def __init__(self, mode): self._mode = mode ...
|
In conftest.py
:
1 2 3 4 5 6 7 8 9 10
| import pytest
def pytest_addoption(parser): parser.addoption( "--test_mode", action="store", default=None, help="my option: type1 or type2" )
@pytest.fixture def test_mode(request): return request.config.getoption("--test_mode")
|