【问题标题】:Pytest - How to Parameterize tests with multiple scenarios?Pytest - 如何参数化具有多个场景的测试?
【发布时间】:2020-08-16 11:52:33
【问题描述】:

我正在使用 pytest 、boto3 和 aws 并希望使用参数化测试进行动态断言。如何改进此代码以仅对一组特定的子网ID 进行断言?

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

【问题讨论】:

    标签: python amazon-web-services unit-testing amazon-dynamodb pytest


    【解决方案1】:

    一种常见的做法是设置环境变量并从中读取,以确定您在哪个平台上运行。

    例如,在环境中你可以有一个变量isProduction=1。然后在您的代码中,您可以通过os.environ['isProduction'] == 1 进行检查。

    出于安全等原因,您甚至可以将私有 ID 保存在环境中。 例如在环境中,您可以在非生产环境中使用以下变量

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

    另外一套在生产机器上

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

    在你做的代码中

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

    因此,您将获得每台机器上的配置。只需确保在您的工作流程/测试流程中环境变量的来源正确。

    【讨论】:

      【解决方案2】:

      如果您需要在参数化上执行额外的逻辑,您可以通过 metafunc 对测试进行参数化。示例:

      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
      

      现在运行 pytest ... 将仅检查非生产 ID,而 NAME="production" pytest ... 将仅检查生产 ID。

      【讨论】:

      • 谢谢,我实际上查询 dynamodb 以检查帐户表中的environment_name。也许我可以写 if (environment =='sandbox ' ) then os.environ['environment'] = 'sandbox'`
      • 您可以将其替换为您认为合适的任何支票,例如if get_environment() contains smth: ...查看您的代码以找到合适的检查。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      相关资源
      最近更新 更多