<!--
title: Manage test environments
domain: cloud-infra
topic: Configurations
language: Python
source: boto/boto3
updated: 2021-12-28
url: https://awesomereviewers.com/reviewers/boto3-manage-test-environments/
-->

When writing tests that interact with environment variables or configuration settings, always (1) preserve the original state and (2) explicitly specify required configurations. 

For environment variables, use setUp/tearDown methods to save and restore their state after test execution:

```python
def restore_dict(self, dictionary: dict, original_data: dict):
    dictionary.clear()
    dictionary.update(original_data)

def setUp(self):
    # Save original environment to restore after test
    original_env = os.environ.copy()
    self.addCleanup(lambda: self.restore_dict(os.environ, original_env))
```

For service configurations, always explicitly specify required parameters like region rather than relying on the machine's environment:

```python
self.session = boto3.session.Session(region_name='us-west-2')
```

This practice ensures tests are isolated, repeatable, and portable across different development environments, preventing unexpected test failures and interference between test cases.
