Skip to content

test: make the flush and logging output tests self-contained - #1386

Open
NoiceHax wants to merge 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-1371
Open

test: make the flush and logging output tests self-contained#1386
NoiceHax wants to merge 1 commit into
mlco2:masterfrom
NoiceHax:fix/issue-1371

Conversation

@NoiceHax

Copy link
Copy Markdown

Description

tests/test_logging_output.py only checks the logger output, but its trackers kept the default CSV output on, so every run also appended a row to emissions.csv in the working directory. _persist_data calls the output handlers in a loop and the CSV one runs first, so once anything went wrong with that shared file the logger handler was never reached. flush() and stop() are wrapped in @suppress(Exception), so nothing was printed and the test failed later on a row count instead. The three trackers there now pass save_to_file=False, and tearDown checks that emissions.csv in the working directory was not touched.

tests/test_emissions_tracker_flush.py pointed every test at one fixed file name under tempfile.gettempdir(), so two tests could end up counting rows in the same file. Each test now gets its own temporary directory.

tests/conftest.py gets an autouse fixture that stops PeriodicScheduler timers a test left armed. The issue asks for that to stay a safety net, so it stays one.

One thing I left out on purpose: _persist_data could wrap each handler.out() call in try/except so a failing output method cannot drop the ones after it. That would have turned this silent failure into a warning, but it changes runtime behaviour, so I would rather do it separately if you want it.

Related Issue

#1371

Motivation and Context

These two test modules shared files with each other and with the rest of the suite, which is why they pass alone and fail in a full run. This removes the shared state. It does not touch library code.

How Has This Been Tested?

Ran the full suite five times on master and five times on this branch, on Windows 11 with Python 3.14. Both sets gave the same numbers: the three tests named in the issue passed every time, and the only failure was test_task_energy_with_live_update_interference, which already fails on master on this machine. So I could not reproduce the intermittent failure here and cannot claim a drop in the failure rate.

What I could reproduce is the coupling behind it. Replace emissions.csv in the repository root with something the CSV handler cannot write to, and all three tests in test_logging_output.py fail on master, even though none of them assert anything about CSV output. On this branch they pass.

Screenshots (if appropriate):

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

AI Usage Disclosure

Please refer to docs/how-to/ai-policy.md for detailed guidelines on how to disclose AI usage in your PR. Accurately completing this section is mandatory.

  • 🟥 AI-vibecoded: You cannot explain the logic. Car analogy : the car drive by itself, you are outside it and just tell it where to go.
  • 🟠 AI-generated: Car analogy : the car drive by itself, you are inside and give instructions.
  • â­� AI-assisted. Car analogy : you drive the car, AI help you find your way.
  • â™»ï¸� No AI used. Car analogy : you drive the car.

An AI agent wrote this patch and this description under my direction, and I checked the reasoning and ran the tests locally before opening the PR.

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the docs/how-to/contributing.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed. See the testing section above: one unrelated test fails on this machine on master as well.

tests/test_logging_output.py only asserts on the logger output, but its
trackers were left with the default CSV output, so every run also appended
to emissions.csv in the working directory. _persist_data calls the handlers
in a loop and the CSV handler runs first, so once anything went wrong with
that shared file the LoggerOutput handler was never reached. flush() and
stop() are wrapped in @Suppress(Exception), so the failure was silent and
showed up later as a row count assertion, which is what mlco2#1371 reports.

Pass save_to_file=False to the three trackers there and check in tearDown
that emissions.csv in the working directory was not touched.

tests/test_emissions_tracker_flush.py pointed every test at one fixed file
name under tempfile.gettempdir(), so two tests counting rows could count in
the same file. Give each test its own temporary directory instead.

Also add an autouse fixture that stops PeriodicScheduler timers a test left
armed. That is only a net, tests are still expected to stop their trackers.
@NoiceHax
NoiceHax requested a review from a team as a code owner August 15, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant