I'm trying to write a test suite for verifying the state of some servers using testinfra.
It's my first time working with python/testinfra/pytest.
As a brief pseudocodey example
test_files.py
testinfra_hosts=[server1,server2,server3]
with open("tests/microservices_default_params.yml", "r") as f:
try:
default_params = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)
with open("tests/" + server + "/params/" + server + "_params.yml", "r") as f:
try:
instance_params = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(exc)
@pytest.mark.parametrize(
"name", [] + default_params["files"] + instance_params["files"]
)
def test_files(host, name):
file = host.file(name)
assert file.exists
Each server has it's own unique params yaml file. I want every server to go through the same test, however I need each server to run the test with its own parametrized values from its respective .yml file.
The problem with the code above, is that it will try to execute all of server1s unique params against both server 2 and 3, then will start again with server 2 being run against servers 1-3 unique params.
I can't find a clean way to essentially have the test run once with server1 as the host, and server 1 params, then do the same again with server2 and server2 params etc.
I've tried using for loops within the test file itself, reading each instance_params.yml into a dictionary with the key being the server name and value containing all of that servers params - but that doesn't smell very good and because the assert is inside the loop, if one of the params for that server fails the loop exits and doesn't attempt any further params for that server.
I've looked into pytest_collection_modifyitems but I can't quite get my head around how to let it do what I want. I feel like there may be an easy solution to this that I'm missing.
My last resort would be to seperate out the tests and parametrized params individually as
@pytest.mark.parametrize(
"server1_params", instance_params['server1']['files]
)
def_test_files_server1(host, server1_params):
...
@pytest.mark.parametrize(
"server2_params", instance_params['server2']['files]
)
def_test_files_server2(host,server2_params):
...
That approach doesn't sound right to me though.
Any help for a fresh junior would be appreciated, I've never asked anything here before Hope it makes sense :)
Aucun commentaire:
Enregistrer un commentaire