0

I want to set a variable in my Python code to different values, according to whether it is running in unit tests or in production.

Assuming we can't pass additional parameters to the test runners, or we don't have any environment variables set in production environment.

A_LOCAL_VARIABLE = 1 if is_running_by_pytest() else 0

My expectation is that by checking whether the code is running under the pytest runner, I can change the value of the variable.

Ideally, if the code is ran by the pytest runner, there're some pre-defined environment variables that we can use to identify it. But I cannot find documentations, or I was using the wrong keywords.

Googi
  • 426
  • 4
  • 16
Wang Xiaoming
  • 19
  • 1
  • 5
  • 1
    Why do you want to do this? There's almost certainly a better way to solve your underlying problem. Code that behaves differently if it detects it's being tested is very difficult to correctly test. – user2357112 Oct 08 '19 at 04:29
  • My root problem is that the value of A_LOCAL_VARIABLE has a huge negative impact on performance under different ENVs. Let's say, it's a value, like 1024, specifying data partition numbers. In production, it has good impact on performance because the data volume is large. However, in unit tests, we use much smaller datasets and the tests are slowed down because we specified too many data partitions. So I want to set it to different values for testing and production, like 1024 for production and 1 for unit tests. – Wang Xiaoming Oct 08 '19 at 04:31
  • Why not make it a parameter? – user2357112 Oct 08 '19 at 04:37

1 Answers1

0

You can use a configuration variable. Each of our executable environments have its own configuration file. Some of our tests use different values for different environments like Single Sign On tests.

jcmack
  • 239
  • 1
  • 11