Use case : check ssh connectivity to multiple clusters in one test
> pytest --collect-only
collected 2 items
<Module Utility_test.py'>
<Function 'test_hello[10.30.107.85]'>
<Function 'test_hello[10.20.91.148]'>
#!/usr/bin/python
import paramiko
import pytest
@pytest.fixture(params=["ip1.ip1.ip1.ip1", "ip2.ip2.ip2.ip2"]) def ssh(request): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(request.param,username="username",key_filename="connect.pem") yield ssh ssh.close() def test_hello(ssh): stdin, stdout, stderr = ssh.exec_command("echo hello") stdin.close() stderr.read() == b"" assert stdout.read() == b"hello\n"
@pytest.fixture(params=["ip1.ip1.ip1.ip1", "ip2.ip2.ip2.ip2"]) def ssh(request): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(request.param,username="username",key_filename="connect.pem") yield ssh ssh.close() def test_hello(ssh): stdin, stdout, stderr = ssh.exec_command("echo hello") stdin.close() stderr.read() == b"" assert stdout.read() == b"hello\n"
run pytest in the terminal to test this.
to check plan of pytest run :
pytest --collect-only
Fixture does the setup of environment before we execute our test cases , in this case since there are multiple parameters , you can find more than one call for that method in the test plan.
To use a fixture in our method, just pass the method name in the function arguments
> pytest --collect-only
collected 2 items
<Module Utility_test.py'>
<Function 'test_hello[10.30.107.85]'>
<Function 'test_hello[10.20.91.148]'>