Skip to content

Add pytest plugin for automatic .coco test compilation (#888)#902

Open
ad4mf06 wants to merge 1 commit intoevhub:developfrom
ad4mf06:feature/pytest-auto-compilation-888
Open

Add pytest plugin for automatic .coco test compilation (#888)#902
ad4mf06 wants to merge 1 commit intoevhub:developfrom
ad4mf06:feature/pytest-auto-compilation-888

Conversation

@ad4mf06
Copy link

@ad4mf06 ad4mf06 commented Mar 3, 2026

Summary

This PR implements issue #888 by adding support for automatic compilation and execution of .coco files via pytest, without requiring a prior manual compilation step.

Changes

pytest_plugin.py (new file)

Created a dedicated pytest plugin containing three core functions:

  • pytest_configure — Called at the start of the pytest session. Installs _PytestCoconutImporter at the front of sys.meta_path to intercept imports of .coco modules.
  • pytest_collect_file — Called for each discovered file. If the file matches test_*.coco, it compiles it to a .py file alongside the source (not in a cache directory), then instructs pytest to collect that .py file. This resolves the import file mismatch error.
  • pytest_ignore_collect — Prevents double-collection by ignoring the coconut_cache/ directory and any .py files that have a corresponding .coco source file.

api.py

Added a function to allow manual installation of the pytest11 entry point, enabling the plugin to be registered directly without extra configuration.

setup.py / pyproject.toml

Registered the plugin via pytest11 so it is automatically installed and activated when the package is installed.

_PytestCoconutImporter — 2 overridden methods

Inherits from CoconutImporter with two overrides:

  • compile() — Replaces sys.stdin during compilation to avoid OSError caused by pytest capturing stdin.
  • find_spec() — Returns a spec pointing to the compiled .py file in place, preventing the global importer from redirecting imports elsewhere.

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