Pytest - How to Parameterize tests with multiple scenarios?

倾然丶 夕夏残阳落幕 提交于 2021-01-29 17:55:28

问题


I'm using pytest ,boto3 and aws and want to have dynamic assertions with parameterized tests. How to improve this code to only assert on a specific group of subnetids?

production_private_ids = ["subnet-08f6d70b65b5cxx38", "subnet-0b6aaaf1ce207xx03", "subnet-0e54fda8f811fxxd8"]) ....
nonproduction_private_ids = ["subnet-11f6xx0b65b5cxx38", "subnet-116aaaf1ce207xx99", "subnet-11xxfda8f811fxx77"]) ....


@pytest.mark.parametrize("subnet", ["production_private_ids", "nonproduction_private_ids", "nonproduction_public_ids","production_public_ids ")

# if environment = production, then only check if production_private_ids exists in team_subnet

def test_sharing_subnets_exist(subnet,accountid):
    team_subnet =  get_team_subnets(accountid)
    assert subnet in team_subnet


# if environment = nonproduction, then only check if nonproduction_private_ids exists in team_subnet
def test_sharing_subnets_exist(subnet,accountid):
    team_subnet =  get_team_subnets(accountid)
    assert subnet in team_subnet

回答1:


One common practice is to set and read from environment variables to determine which platform you are running from.

For example, in the environment you can have a variable isProduction=1. Then in your code, you can check by os.environ['isProduction'] == 1.

You can even save the private ids in the environment for reasons such as safety. For example in the environment, you can have the following variables on nonproduction

id1="subnet-11f6xx0b65b5cxx38"
id2="subnet-116aaaf1ce207xx99"
id3"subnet-11xxfda8f811fxx77"

And another set on production machine

id1="subnet-08f6d70b65b5cxx38"
id2="subnet-0b6aaaf1ce207xx03"
id3="subnet-0e54fda8f811fxxd8"

in the code you do

import os
private_ids = [os.environ['id1'], os.environ['id2'], os.environ['id3']]

So you'll get the configs on each machine. Just make sure in your workflow/testflow the environment variables are correctly sourced.




回答2:


You can parametrize the tests via metafunc if you need to execute additional logic on parametrization. Example:

import os
import pytest

production_private_ids = [...]
nonproduction_private_ids = [...]


def pytest_generate_tests(metafunc):
    # if the test has `subnet` in args, parametrize it now
    if 'subnet' in metafunc.fixturenames:
        # replace with your environment check
        if os.environ.get('NAME', None) == 'production':
            ids = production_private_ids
        else:
            ids = nonproduction_private_ids
        metafunc.parametrize('subnet', ids)


def test_sharing_subnets_exist(subnet, accountid):
    team_subnet =  get_team_subnets(accountid)
    assert subnet in team_subnet

Now running pytest ... will check only non-production IDs, while NAME="production" pytest ... will check only production IDs.



来源:https://stackoverflow.com/questions/63436542/pytest-how-to-parameterize-tests-with-multiple-scenarios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!