Skip to content

Commit afb13a0

Browse files
authored
Merge pull request #5 from jsmedmar/master
feat: add ability to use env-vars with a python-like syntax.
2 parents 8f5339a + 54138b8 commit afb13a0

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
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: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
1-
import os
1+
"""Adopt environment section in pytest configuration files."""
22

3+
import os
34
import pytest
45

56

67
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=[])
8+
"""Add section to configuration files."""
9+
help_msg = (
10+
"a line separated list of environment variables "
11+
"of the form NAME=VALUE."
12+
)
13+
14+
parser.addini(
15+
"env",
16+
type="linelist",
17+
help=help_msg,
18+
default=[]
19+
)
1120

1221

1322
@pytest.hookimpl(tryfirst=True)
1423
def pytest_load_initial_conftests(args, early_config, parser):
24+
"""Load environment variables from configuration files."""
1525
for e in early_config.getini("env"):
1626
part = e.partition("=")
1727
key = part[0].strip()
1828
value = part[2].strip()
1929

20-
# use D: as a way to designate a default value
21-
# that will only override env variables if they
30+
# Replace environment variables in value. for instance:
31+
# TEST_DIR={USER}/repo_test_dir.
32+
value = value.format(**os.environ)
33+
34+
# use D: as a way to designate a default value
35+
# that will only override env variables if they
2236
# do not exist already
2337
dkey = key.split("D:")
2438
default_val = False
39+
2540
if len(dkey) == 2:
2641
key = dkey[1]
2742
default_val = True

setup.py

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

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

57
setup(
68
name='pytest-env',
7-
description=description,
8-
long_description=description,
9-
version='0.6.1',
9+
description=DESCRIPTION,
10+
long_description=DESCRIPTION,
11+
version='0.6.2',
1012
1113
author_email='[email protected]',
1214
url='https://github.com/MobileDynasty/pytest-env',

0 commit comments

Comments
 (0)