Using a command-line option in a pytest skip-if condition

后端 未结 2 407
攒了一身酷
攒了一身酷 2021-01-12 22:16

Long story short, I want to be able to skip some tests if the session is being run against our production API. The environment that the tests are run against is set with a c

2条回答
  •  野的像风
    2021-01-12 22:46

    Looks like true way to Control skipping of tests according to command line option is mark tests as skip dynamically:

    1. add option using pytest_addoption hook like this:
    def pytest_addoption(parser):
        parser.addoption(
            "--runslow", action="store_true", default=False, help="run slow tests"
        )
    
    1. Use pytest_collection_modifyitems hook to add marker like this:
    def pytest_collection_modifyitems(config, items):
        if config.getoption("--runslow"):
            # --runslow given in cli: do not skip slow tests
            return
        skip_slow = pytest.mark.skip(reason="need --runslow option to run")
        for item in items:
            if "slow" in item.keywords:
                item.add_marker(skip_slow)
    
    1. Add mark to you test:
    @pytest.mark.slow
    def test_func_slow():
        pass
    

    If you want to use the data from the CLI in a test, for example, it`s credentials, enough to specify a skip option when retrieving them from the pytestconfig:

    1. add option using pytest_addoption hook like this:
    def pytest_addoption(parser):
        parser.addoption(
            "--credentials",
            action="store",
            default=None,
            help="credentials to ..."
        )
    
    1. use skip option when get it from pytestconfig
    @pytest.fixture(scope="session")
    def super_secret_fixture(pytestconfig):
        credentials = pytestconfig.getoption('--credentials', skip=True)
        ...
    
    1. use fixture as usual in you test:
    def test_with_fixture(super_secret_fixture):
        ...
    

    In this case you will got something like this it you not send --credentials option to CLI:

    Skipped: no 'credentials' option found
    

    It is better to use _pytest.config.get_config instead of deprecated pytest.config If you still wont to use pytest.mark.skipif like this:

    @pytest.mark.skipif(not _pytest.config.get_config().getoption('--credentials'), reason="--credentials was not specified")
    

提交回复
热议问题