Skip to content

Commit

Permalink
test: add env_vars pytest.ini option
Browse files Browse the repository at this point in the history
use env_vars key in pytest.ini to set environment variables that
will be set before tests run.
  • Loading branch information
aaraney authored and christophertubbs committed Jan 26, 2024
1 parent 36b5f4c commit bb86d04
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
30 changes: 23 additions & 7 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import logging
import os
import subprocess
import sys
from pathlib import Path
import logging
from typing import Dict, List, Set, TYPE_CHECKING, Optional
from pytest import Item, CallInfo, Session
import subprocess
from typing import TYPE_CHECKING, Dict, List, Optional, Set

import pytest
from pytest import CallInfo, Item, Session

logger = logging.getLogger("dmod_conftest")

Expand Down Expand Up @@ -35,13 +37,16 @@ def pytest_addoption(parser: "Parser"):
parser.addini(
"it_env_vars", "Environment variables for integration tests", type="args"
)
parser.addini(
"env_vars", "Environment variables to set", type="args"
)


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


def pytest_configure(config: "Config"):
def _configure_it_env_vars(config: "Config"):
if config.getoption("it"):
python_files = config.getini("python_files")
assert isinstance(python_files, list)
Expand All @@ -50,11 +55,22 @@ def pytest_configure(config: "Config"):
it_env_vars = config.getini("it_env_vars")
integration_testing_flag()

parsed_vars = parse_it_env_vars(it_env_vars)
parsed_vars = parse_env_vars(it_env_vars)
logger.debug(f"Adding these environment variables: {parsed_vars}")

os.environ.update(parsed_vars)

def _configure_env_vars(config: "Config"):
env_vars = config.getini("env_vars")
assert isinstance(env_vars, list)
parsed_vars = parse_env_vars(env_vars)
logger.debug(f"Adding these environment variables: {parsed_vars}")
os.environ.update(parsed_vars)

def pytest_configure(config: "Config"):
_configure_env_vars(config)
_configure_it_env_vars(config)


def get_setup_script_path(module: Path) -> Path:
return module / "setup_it_env.sh"
Expand Down
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

[pytest]
addopts = --import-mode=importlib
; environment variables that will be added before tests are run
; key=value pairs with no spaces
; env_vars =
; key=value pairs with no spaces
it_env_vars =
; A name for the Docker container used to run a Redis instance during integration testing
Expand Down

0 comments on commit bb86d04

Please sign in to comment.