3

I'm using Python 3.8 and pytest. I have this pytest.ini file ...

[pytest]
env_override_existing_values = 1
env_files =
    tests/.test_env

My tests/.test_env contains

TZ=`ls -la /etc/localtime | cut -d/ -f8-9`

However, this is getting literally evaluated in my pytest -- that is TZ is being equal to "ls -la /etc/localtime | cut -d/ -f8-9". Is there a way I can configure my env var for pytest to be the result of an expression when I run "pytest tests/my_test.py"? FYI, it is not an option to do something like "TZ=ls -la /etc/localtime | cut -d/ -f8-9; pytest tests/my_test.py"

Dave
  • 15,639
  • 133
  • 442
  • 830
  • What happens when you run this on $ prompt: `TZ=\`ls -la /etc/localtime | cut -d/ -f8-9\` pytest tests/mmy_test.py`. Apart from that, are you sure you are getting the correct field #s (`8-9`) for what you are looking for. See this for more info: https://stackoverflow.com/questions/36141024/how-to-pass-environment-variables-to-pytest + how are you passing the `-c your.ini` file? – AKS Aug 05 '20 at 22:46
  • As .test_env is not a shell script, it won't set TZ variable like you are thinking (how shell will set it to). You can probably do something like `env:` and set `TZ=$TZ` if TZ is exported. – AKS Aug 05 '20 at 22:55
  • 1
    This plugin looks extremely simple and clearly [does not support shell escape](https://github.com/JonnyFunFun/pytest-envfiles/blob/master/pytest_envfiles/plugin.py). My question is, why do you have to take this from the environment? You want to put it in env file just to read from env file again? Can't you cut out the middleman and just read the file `/etc/localtime` from within Python code? – wim Aug 06 '20 at 03:29
  • I'm also using a plugin that spawns a Docker container in which a database is brought up. I need the Docker container to have the same timezone as my local machine. I wanted to pass an env variable to the docker container to tell it what timezone to use. I'm happy to use a different env plugin if it can take care of what I need. – Dave Aug 06 '20 at 17:15

3 Answers3

1

So, if I understand this correctly, you want to set an envvar within the pytest process, without using any outside parameters. You could use a session scoped pytest fixture in conftest.py like this:

$ cat conftest.py
import os
import subprocess

import pytest


@pytest.fixture(scope="session", autouse=True)
def setenv():
    process = subprocess.run("ls -la /etc/localtime | cut -d/ -f8-9", shell=True, capture_output=True)
    os.environ["TZ"] = process.stdout.decode("utf8")

$ cat test_foo.py
import os


def test_my_test():
    print(os.environ["TZ"])
$ pytest -s
================================================================================= test session starts ==================================================================================
platform linux -- Python 3.8.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /tmp/testy
collected 1 item                                                                                                                                                                       

test_foo.py New York

.

================================================================================== 1 passed in 0.01s ===================================================================================
$ echo $TZ

$
Oin
  • 6,951
  • 2
  • 31
  • 55
0

The (pytest documentation)[https://pypi.org/project/pytest-envfiles/] suggests the files used by the pytest "env_files" directive need to contain literal key=val lines. Yes, the documentation is not clear regarding the format for pytest env files. So your, incorrect, expectation those files are "evaluated" using your $SHELL program, let alone in any other fashion, is not surprising.

I don't understand why you can't do something like "TZ=ls -la /etc/localtime | cut -d/ -f8-9; pytest tests/my_test.py". Also, you are apparently trying to explicitly set $TZ to the default timezone for the platform. Why? That should be the default. Which suggests there is a deeper problem. That is, you are asking a xyproblem.info question.

Kurtis Rader
  • 6,734
  • 13
  • 20
  • I am using a PyTest plugin that spins up a docker container. I need to set the timezone of the docker container to be the local machine. – Dave Aug 06 '20 at 12:33
-1

So basically pytest.ini may contain:

[pytest]
TZ=

Then, you can pass it at cmd line as:

$ TZ=`ls -la /etc/localtime | cut -d/ -f8-9` pytest -c pytest.ini -s tests/**
AKS
  • 16,482
  • 43
  • 166
  • 258
  • 1
    Thanks, but you didn't read the last sentence of my question ("FYI, it is not an option to do something ...") ;) – Dave Aug 05 '20 at 23:21