Skip to content

Commit 4462c24

Browse files
author
Juan Medina
committed
🐋 feat: add ability to use env-vars with a python-like syntax in env section.
This commit enables the use environment variables in the test env configuration using: @pytest.hookimpl(tryfirst=True) def pytest_load_initial_conftests(args, early_config, parser): ... # Replace environment variables in value. for instance: # RUN_PATH=/run/path/{USER} value = value.format(**os.environ) ... This functionality has been documented in the README.md file. Additionally, I added some simple docs and pep8 styling changes. I think this change is worth increasing the version to 0.6.2, I updated the `setup.py` accordingly.
1 parent 8f5339a commit 4462c24

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
pytest-env
2-
=================
1+
# pytest-env
32

43
This is a py.test plugin that enables you to set environment variables in the pytest.ini file.
54

6-
Installation
7-
------------
5+
## Installation
86

9-
Install with pip::
7+
Install with pip:
108

119
pip install pytest-env
1210

13-
Uninstall with pip::
11+
Uninstall with pip:
1412

1513
pip uninstall pytest-env
1614

17-
Usage
18-
-----
15+
## Usage
1916

20-
In your pytest.ini file add a key value pair with `env` as the key and the environment variables as a line
21-
separated list of `KEY=VALUE` entries. The defined variables will be added to the environment before any tests are run:
17+
In your pytest.ini file add a key value pair with `env` as the key and the environment variables as a line separated list of `KEY=VALUE` entries. The defined variables will be added to the environment before any tests are run:
2218

2319
[pytest]
24-
env =
20+
env =
2521
HOME=~/tmp
2622
RUN_ENV=test
2723

2824
You can use `D:` (default) as prefix if you don't want to override existing environment variables:
2925

30-
3126
[pytest]
32-
env =
27+
env =
3328
D:HOME=~/tmp
3429
D:RUN_ENV=test
3530

31+
Lastly, you can use existing environment variables using a python-like format:
32+
33+
[pytest]
34+
env =
35+
RUN_PATH=/run/path/{USER}

pytest_env/plugin.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
1-
import os
21

2+
"""Adopt environment section in pytest configuration files."""
3+
4+
import os
35
import pytest
46

57

68
def pytest_addoption(parser):
7-
parser.addini("env",
8-
type="linelist",
9-
help="a line separated list of environment variables of the form NAME=VALUE)",
10-
default=[])
9+
"""Add section to configuration files."""
10+
help_msg = (
11+
"a line separated list of environment variables "
12+
"of the form NAME=VALUE."
13+
)
14+
15+
parser.addini(
16+
"env",
17+
type="linelist",
18+
help=help_msg,
19+
default=[]
20+
)
1121

1222

1323
@pytest.hookimpl(tryfirst=True)
1424
def pytest_load_initial_conftests(args, early_config, parser):
25+
"""Load environment variables from configuration files."""
1526
for e in early_config.getini("env"):
1627
part = e.partition("=")
1728
key = part[0].strip()
1829
value = part[2].strip()
1930

20-
# use D: as a way to designate a default value
21-
# that will only override env variables if they
31+
# Replace environment variables in value. for instance:
32+
# TEST_DIR={USER}/repo_test_dir.
33+
value = value.format(**os.environ)
34+
35+
# use D: as a way to designate a default value
36+
# that will only override env variables if they
2237
# do not exist already
2338
dkey = key.split("D:")
2439
default_val = False
40+
2541
if len(dkey) == 2:
2642
key = dkey[1]
2743
default_val = True

setup.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
2+
"""pytest-env setup module."""
3+
14
from setuptools import setup
25

3-
description = 'py.test plugin that allows you to add environment variables.'
6+
DESCRIPTION = 'py.test plugin that allows you to add environment variables.'
47

58
setup(
69
name='pytest-env',
7-
description=description,
8-
long_description=description,
9-
version='0.6.1',
10+
description=DESCRIPTION,
11+
long_description=DESCRIPTION,
12+
version='0.6.2',
1013
1114
author_email='[email protected]',
1215
url='https://github.com/MobileDynasty/pytest-env',
@@ -23,5 +26,5 @@
2326
'Programming Language :: Python :: 2.7',
2427
'Programming Language :: Python :: 3',
2528
'Programming Language :: Python :: 3.3',
26-
]
27-
)
29+
]
30+
)

0 commit comments

Comments
 (0)