2

I am using testinfra to test if some services is running/enabled on different nodes. Testinfra can use ansible for getting the nodes to test and it will also make ansible groups available. Testinfra uses pytest.

I only want to test for services on very few ansible groups and the code looks like this.

import pytest
import testinfra

group_service_map = [
    ('group1', 'svc1'),
    ('group1', 'svc2'),
    ('group2', 'svc3')
]

@pytest.fixture
def host_groups(host):
    return host.ansible.get_variables()['group_names']

@pytest.mark.parametrize('check', ('running', 'enabled'))
@pytest.mark.parametrize('group, service', group_service_map)
def test_service_running(host, check, host_groups, group, service):
    if group in host_groups:
        assert getattr(host.service(service), f'is_{check}') is True, f'{service} is not {check}'

This works great, except that pytest things that I got 1 passing test for each time test_service_running is called (which is a lot, since this is a parametrized test). I tried adding a pytest.skip(), but then pytest shows the tests as skipped (yellow s).

The goal is to ignore the test in the output, not just skip it. So instead of a green or yellow ., I want nothing.

I've read the api-docs and in the source, but can't find any info if this is even possible.

I'll make a pull-request, but wanted to check first if anyone know of a solution.

xeor
  • 5,301
  • 5
  • 36
  • 59
  • I'm not sure at what point you know which tests to skip, but maybe you can implement `pytest_collection_modifyitems` and remove the tests in question? – MrBean Bremen May 29 '20 at 06:49
  • Thanks for the suggestion. I'll check it takes. I know inside the test where the if statement is. As far as I know, i can't use fixtures inside skip_if, so I have no where to do it beforehand – xeor May 29 '20 at 06:57
  • If you know this only inside the test, I think this is not possible - you can only skip. The only possibility is probably to move the check outside of the test, if that is possible (or change pytest, of course :). If you just don't want to see skipped tests, you may remove them from the test report afterwards, depending on your workflow. – MrBean Bremen May 29 '20 at 07:16

0 Answers0