简体   繁体   English

pytest-使用环境特定的值设置环境变量

[英]pytest - set environment variables with environment specific values

I am using pytest to run tests for my python services that run in AWS. 我正在使用pytest对在AWS中运行的python服务运行测试。

I am trying to test a service which triggers from an SNS topic. 我正在尝试测试从SNS主题触发的服务。 So my test writes to the SNS topic. 所以我的测试写了SNS主题。 The topic ARN is different for dev, staging, prod. ARN主题对于dev,staging和prod是不同的。

In the test I would like to be able to access the topic ARN using the same environment variable and pick up the right topic for each environment. 在测试中,我希望能够使用相同的环境变量访问主题ARN,并为每种环境选择合适的主题。

self.sns_topic_arn = os.environ['SNS_TOPIC']

I have a service which does set, build, test which is independent of all my services and deployment environments. 我提供的服务确实可以独立于我的所有服务和部署环境进行设置,构建,测试。

I refer to this discussion on how to pass the env vars How to pass environment variables to pytest 我参考了有关如何传递环境变量的讨论,如何将环境变量传递给pytest

However I am not able to figure out how to set the value per environment. 但是我不知道如何设置每个环境的值。 Is there a template or a best practice? 是否有模板或最佳做法?

I am posting the solution which works for us. 我正在发布对我们有用的解决方案。 I am not sure this would be a useful answer in all use cases or that my question applies to many users. 我不确定这在所有用例中是否都是有用的答案,或者我的问题是否适用于许多用户。 The way our test & deployment is set up, this turned out to be simple. 事实证明,我们测试和部署的设置方式很简单。 I didn't figure it out right away to start with :|. 我没有马上就知道它是从:|开始的。

I have a service which does set, build, test which is independent of all my services and deployment environments. 我提供的服务确实可以独立于我的所有服务和部署环境进行设置,构建,测试。

The service runs these tests in a docker container. 该服务在docker容器中运行这些测试。 The tests are run as acceptance tests using AWS code pipeline and there's a Docker config for each environment. 这些测试使用AWS代码管道作为验收测试运行,并且每个环境都有一个Docker配置。 So we are able to set the right 'SNS_TOPIC' as the environment variable in each config. 因此,我们能够在每个配置中将正确的“ SNS_TOPIC”设置为环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM