pytest-testinfra icon indicating copy to clipboard operation
pytest-testinfra copied to clipboard

Exclude or Include hosts per test function

Open philpep opened this issue 8 years ago • 5 comments

Workaround http://pastebin.com/TqGfqVZ3

philpep avatar Jan 11 '16 22:01 philpep

Well, I wanted to be able to skip a test based on a remote command. Here the command was 'hostname -s', but it could have been anything else. Is there a way to do that?

emayssat-ms avatar Sep 18 '16 19:09 emayssat-ms

Doesn't @pytest.mark.testinfra_hosts already do something like that? See https://github.com/philpep/testinfra/blob/master/testinfra/test/test_backends.py

stefreak avatar Nov 10 '16 18:11 stefreak

@pytest.mark.testinfra_hosts does not work with Salt, we had to use the suggested workaround. My team would be happy to extend @pytest.mark.testinfra_hosts to support Salt. Could you point us in the right direction? Thanks a lot.

pierluca avatar Nov 09 '17 14:11 pierluca

Do you have an idea @philpep? :)

stefreak avatar Nov 09 '17 15:11 stefreak

FWIW, a (more ansible specific) workaround. My use case is ansible group intersection. I'm pretty new to python and to pytest, but this seems to be elegant. The approach is to define a conftest.py with a fixture:

@pytest.fixture(scope='class')
    def groups(host):
    return host.ansible.get_variables()['group_names']

Then add groups to the test function parameters, and add a line like if 'GROUP_NAME' in groups:

This avoids pytest.skip() which, for my usage is a plus.

fourjay avatar Jan 16 '20 22:01 fourjay