diff --git a/core/dbt/tests/fixtures/project.py b/core/dbt/tests/fixtures/project.py index 3ef25c55789..252f11116c1 100644 --- a/core/dbt/tests/fixtures/project.py +++ b/core/dbt/tests/fixtures/project.py @@ -1,5 +1,7 @@ import os from pathlib import Path +from typing import Mapping + import pytest # type: ignore import random from argparse import Namespace @@ -493,11 +495,18 @@ def get_tables_in_schema(self): return {model_name: materialization for (model_name, materialization) in result} +@pytest.fixture(scope="class") +def environment() -> Mapping[str, str]: + # By default, fixture initialization is done with the following environment + # from the os, but this fixture provides a way to customize the environment. + return os.environ + + # Housekeeping that needs to be done before we start setting up any test fixtures. @pytest.fixture(scope="class") -def initialization() -> None: +def initialization(environment) -> None: # Create an "invocation context," which dbt application code relies on. - set_invocation_context(os.environ) + set_invocation_context(environment) # Enable caches used between test runs, for better testing performance. enable_test_caching() diff --git a/tests/functional/partial_parsing/test_pp_vars.py b/tests/functional/partial_parsing/test_pp_vars.py index aa93f1131dd..e55592f8dd2 100644 --- a/tests/functional/partial_parsing/test_pp_vars.py +++ b/tests/functional/partial_parsing/test_pp_vars.py @@ -264,11 +264,16 @@ def test_env_vars_models(self, project): class TestProjectEnvVars: + @pytest.fixture(scope="class") + def environment(self): + custom_env = os.environ.copy() + custom_env["ENV_VAR_NAME"] = "Jane Smith" + return custom_env + @pytest.fixture(scope="class") def project_config_update(self): # Need to set the environment variable here initially because # the project fixture loads the config. - os.environ["ENV_VAR_NAME"] = "Jane Smith" return {"models": {"+meta": {"meta_name": "{{ env_var('ENV_VAR_NAME') }}"}}} @pytest.fixture(scope="class") @@ -279,6 +284,7 @@ def models(self): def test_project_env_vars(self, project): # Initial run + os.environ["ENV_VAR_NAME"] = "Jane Smith" results = run_dbt(["run"]) assert len(results) == 1 manifest = get_manifest(project.project_root) @@ -308,13 +314,14 @@ def models(self): "model_one.sql": model_one_sql, } + @pytest.fixture(scope="class") + def environment(self): + custom_env = os.environ.copy() + custom_env["ENV_VAR_HOST"] = "localhost" + return custom_env + @pytest.fixture(scope="class") def dbt_profile_target(self): - # Need to set these here because the base integration test class - # calls 'load_config' before the tests are run. - # Note: only the specified profile is rendered, so there's no - # point it setting env_vars in non-used profiles. - os.environ["ENV_VAR_HOST"] = "localhost" return { "type": "postgres", "threads": 4,