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

How to setup test ?

Open spham opened this issue 4 years ago • 1 comments

Hi How To setup or arrange test before , when Arrange test ? Example here. I need To start zookeeper before test kafka

def test_zookeeper_service(host):
     host.run('sudo systemctl start zookeeper')
     z = host.service("kafka")
     assert z.is_running
     assert z.is_enable

spham avatar Dec 09 '21 04:12 spham

Hi, dynamically starting/stopping services during tests could require to wait/retry for instance to wait up to 10 second the service is running:

host.run("sudo systemctl start zookeeper")
zookeeper = host.service("zookeeper")
for _ in range(10):
    if zookeeper.is_running:
        break
    time.sleep(1)
else:
    raise RuntimeError("Zookeeper not started!")

If you need something more configurable to wait there's might be advanced libraries for this like https://github.com/jd/tenacity

Then if you need to temporary start service for a test but expect the service is not running for other tests, you can define a fixture for this:

@pytest.fixture
def zookeper_running(host):
    host.run("sudo systemctl start zookeeper")
    # [...] wait zookeeper is running
    yield
    host.run("sudo systemctl stop zookeeper")
    # [...] wait zookeeper is stopped

@pytest.mark.usefixtures("zookeeper_running")
def test_kafka(host):
    # [...]

philpep avatar Dec 09 '21 09:12 philpep