Skip to content

Commit 55e50d5

Browse files
committed
test: add env_vars pytest.ini option
use env_vars key in pytest.ini to set environment variables that will be set before tests run.
1 parent bf4ca4d commit 55e50d5

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

conftest.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import logging
12
import os
3+
import subprocess
24
import sys
35
from pathlib import Path
4-
import logging
5-
from typing import Dict, List, Set, TYPE_CHECKING, Optional
6-
from pytest import Item, CallInfo, Session
7-
import subprocess
6+
from typing import TYPE_CHECKING, Dict, List, Optional, Set
7+
8+
import pytest
9+
from pytest import CallInfo, Item, Session
810

911
logger = logging.getLogger("dmod_conftest")
1012

@@ -35,13 +37,16 @@ def pytest_addoption(parser: "Parser"):
3537
parser.addini(
3638
"it_env_vars", "Environment variables for integration tests", type="args"
3739
)
40+
parser.addini(
41+
"env_vars", "Environment variables to set", type="args"
42+
)
3843

3944

40-
def parse_it_env_vars(name_values: List[str]) -> Dict[str, str]:
45+
def parse_env_vars(name_values: List[str]) -> Dict[str, str]:
4146
return dict(map(lambda pair: pair.split("="), name_values))
4247

4348

44-
def pytest_configure(config: "Config"):
49+
def _configure_it_env_vars(config: "Config"):
4550
if config.getoption("it"):
4651
python_files = config.getini("python_files")
4752
assert isinstance(python_files, list)
@@ -50,11 +55,22 @@ def pytest_configure(config: "Config"):
5055
it_env_vars = config.getini("it_env_vars")
5156
integration_testing_flag()
5257

53-
parsed_vars = parse_it_env_vars(it_env_vars)
58+
parsed_vars = parse_env_vars(it_env_vars)
5459
logger.debug(f"Adding these environment variables: {parsed_vars}")
5560

5661
os.environ.update(parsed_vars)
5762

63+
def _configure_env_vars(config: "Config"):
64+
env_vars = config.getini("env_vars")
65+
assert isinstance(env_vars, list)
66+
parsed_vars = parse_env_vars(env_vars)
67+
logger.debug(f"Adding these environment variables: {parsed_vars}")
68+
os.environ.update(parsed_vars)
69+
70+
def pytest_configure(config: "Config"):
71+
_configure_env_vars(config)
72+
_configure_it_env_vars(config)
73+
5874

5975
def get_setup_script_path(module: Path) -> Path:
6076
return module / "setup_it_env.sh"

pytest.ini

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
12
[pytest]
23
addopts = --import-mode=importlib
4+
; environment variables that will be added before tests are run
5+
; key=value pairs with no spaces
6+
; env_vars =
37
; key=value pairs with no spaces
48
it_env_vars =
59
; A name for the Docker container used to run a Redis instance during integration testing

0 commit comments

Comments
 (0)