Skip to content

Commit

Permalink
[Train] Monkeypatch environment variables in test_json (ray-project…
Browse files Browse the repository at this point in the history
…#21260)

If we use `os.environ` to set environment variables in tests, then our tests become coupled. By using `monkeypatch`, we can safely set environment variables while ensuring our tests remain decoupled. 

For more information, see the [monkeypatching documentation](https://docs.pytest.org/en/6.2.x/monkeypatch.html#monkeypatching-environment-variables).
  • Loading branch information
bveeramani authored Jan 3, 2022
1 parent 7ce22b7 commit fa4e41c
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions python/ray/train/tests/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def on_shutdown(self, worker_group: WorkerGroup,
@pytest.mark.parametrize("workers_to_log", [0, None, [0, 1]])
@pytest.mark.parametrize("detailed", [False, True])
@pytest.mark.parametrize("filename", [None, "my_own_filename.json"])
def test_json(ray_start_4_cpus, make_temp_dir, workers_to_log, detailed,
filename):
def test_json(monkeypatch, ray_start_4_cpus, make_temp_dir, workers_to_log,
detailed, filename):
if detailed:
os.environ[ENABLE_DETAILED_AUTOFILLED_METRICS_ENV] = "1"
monkeypatch.setenv(ENABLE_DETAILED_AUTOFILLED_METRICS_ENV, "1")

config = TestConfig()

Expand Down Expand Up @@ -119,9 +119,6 @@ def train_func():
all(not any(key in worker for key in DETAILED_AUTOFILLED_KEYS)
for worker in element) for element in log)

os.environ.pop(ENABLE_DETAILED_AUTOFILLED_METRICS_ENV, 0)
assert ENABLE_DETAILED_AUTOFILLED_METRICS_ENV not in os.environ


def _validate_tbx_result(events_dir):
events_file = list(glob.glob(f"{events_dir}/events*"))[0]
Expand Down

0 comments on commit fa4e41c

Please sign in to comment.