Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tests more robust to the running environment #464

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ def docs_test_env():
yield setenv

setenv.clear()


@pytest.fixture
def cli_test_env():
setenv = SetEnv()

# envs for reproducible cli tests
setenv.set('COLUMNS', '80')

yield setenv

setenv.clear()
15 changes: 13 additions & 2 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from enum import IntEnum
from pathlib import Path
from typing import Any, Callable, Dict, Generic, Hashable, List, Optional, Set, Tuple, Type, TypeVar, Union
from unittest import mock

import pytest
from annotated_types import MinLen
Expand Down Expand Up @@ -68,6 +69,12 @@ class SettingWithPopulateByName(BaseSettings):
model_config = SettingsConfigDict(populate_by_name=True)


@pytest.fixture(autouse=True)
def clean_env():
with mock.patch.dict(os.environ, clear=True):
yield


def test_sub_env(env):
env.set('apple', 'hello')
s = SimpleSettings()
Expand Down Expand Up @@ -1109,9 +1116,13 @@ class Settings(BaseSettings):


@pytest.fixture
def home_tmp():
def home_tmp(tmp_path, env):
env.set('HOME', str(tmp_path))
env.set('USERPROFILE', str(tmp_path))
env.set('HOMEPATH', str(tmp_path))

tmp_filename = f'{uuid.uuid4()}.env'
home_tmp_path = Path.home() / tmp_filename
home_tmp_path = tmp_path / tmp_filename
yield home_tmp_path, tmp_filename
home_tmp_path.unlink()

Expand Down
5 changes: 5 additions & 0 deletions tests/test_source_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
ARGPARSE_OPTIONS_TEXT = 'options' if sys.version_info >= (3, 10) else 'optional arguments'


@pytest.fixture(autouse=True)
def cli_test_env_autouse(cli_test_env):
pass


def foobar(a, b, c=4):
pass

Expand Down
Loading