Skip to content

perf: Speed up unit tests by optimizing fixture scoping #81

@Jammy2211

Description

@Jammy2211

Overview

PyAutoConf's 71 unit tests take ~10.5s due to redundant conf.Config instantiation. The config fixture is function-scoped, meaning every test rebuilds the Config from disk via recursive filesystem traversal. Read-only tests can share a session-scoped config, cutting total time by ~50-60%.

This is the first repo in a series — the same profiling and optimization approach will be applied to PyAutoFit, PyAutoArray, PyAutoGalaxy, and PyAutoLens.

Plan

  • Profile test suite to identify slowest tests and root causes
  • Categorize tests into read-only (config lookups) vs mutating (config.push)
  • Introduce session-scoped config fixture for read-only tests
  • Keep function-scoped fixture for tests that mutate config state
  • Verify all 71 tests pass with identical assertions — no numerical changes
Detailed implementation plan

Affected Repositories

  • PyAutoConf (primary)

Work Classification

Library

Branch Survey

Repository Current Branch Dirty?
./PyAutoConf main 2 untracked .fits

Suggested branch: feature/speed-up-unit-tests

Implementation Steps

  1. Refactor test_autoconf/conftest.py — Add a session-scoped config fixture (_shared_config) and a read-only wrapper. Keep the existing function-scoped config for mutating tests.

  2. Update test_autoconf/test_default.py — Switch read-only tests (test_override_file, test_override_in_directory, test_novel_directory, test_novel_file, test_json, test_embedded_yaml_default, test_as_dict, test_mix_files) to use the shared config. Keep test_push, test_keep_first, test_logging_config on function-scoped fixture.

  3. Review test_autoconf/test_config.py — All tests are read-only lookups, can use session-scoped config.

  4. Review test_autoconf/test_decorator.py and test_autoconf/test_output_config.py — These have autouse fixtures that push config; analyse whether they can be scoped higher.

  5. Review test_autoconf/test_dictable.py, test_autoconf/test_fitsable.py — These don't use the config fixture; no changes needed.

  6. Run full test suite and compare timing before/after.

Key Files

  • test_autoconf/conftest.py — fixture refactoring
  • test_autoconf/test_default.py — largest speedup target (8 of 11 tests are read-only)

Original Prompt

Click to expand starting prompt

This is a generic source code improvement that applies to all five PyAuto repos.

Basically, some unit tests run slow, and this dev work aims to profile the unit tests and speed them up as much as possible. We don't want to change numerical values on unit tests where possible, however I am also confident currently that the source code is stable thus for the slower unit tests if there are safe changes we can make to speed them up, then we should do so, but prompt me on them.

To begin, can we run this on PyAutoConf and I'll see how it goes and we can then run it on the other repos.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions