Hi, are there any examples of testing out the conf...
# prefect-community
d
Hi, are there any examples of testing out the config functionality from unit tests? I tried the below but config.environment yields a BoxKeyError ("Config object has no attribute environment")
Copy code
def test_configuration_works_as_expected(monkeypatch, tmp_path):
    config_file = tmp_path / "config.toml"
    config_text = """
environment = "prod"
user = "${environments.${environment}.user}"

[environments]

    [environments.dev]
        user = "test"

    [environments.prod]
        user = "admin"
"""
    with config_file.open("w") as cf:
        cf.write(config_text)

    monkeypatch.setenv("PREFECT__USER_CONFIG_PATH", str(tmp_path))
    assert config.environment == "prod"
    assert config.user == "admin"
c
Hi Dolor - check out Prefect’s own configuration unit tests: https://github.com/PrefectHQ/prefect/blob/master/tests/test_configuration.py In short, config is loaded at first-import time, so you’ll need to call
prefect.configuration.load_configuration
explicitly within your tests
d
ah, yes that makes sense
trying now!
hrm, calling load_configuration explicitly didn't do the trick.
Copy code
def test_configuration_works_as_expected(tmp_path):
    config_file = tmp_path / "config.toml"
    config_text = """
environment = "prod"
user = "${environments.${environment}.user}"

[environments]

    [environments.dev]
        user = "test"

    [environments.prod]
        user = "admin"
"""
    with config_file.open("w") as cf:
        cf.write(config_text)

    load_configuration(path=str(config_file))
    assert config.environment == "prod"
    assert config.user == "admin"
environment isn't in the config object after load_configuration completes.
ah. it returns the config object.
(assumed it was changing the global prefect.config, but this is preferable)
looking good