Skip to content

[testing] Provider overrides, teardown and mocks in NitroTestingModule - #102

Open
ROHIT-JR wants to merge 1 commit into
nitrocloudofficial:developfrom
ROHIT-JR:feat/74-testing-overrides
Open

ROHIT-JR wants to merge 1 commit into
nitrocloudofficial:developfrom
ROHIT-JR:feat/74-testing-overrides

Conversation

@ROHIT-JR

Copy link
Copy Markdown
Collaborator

Closes #74

What this adds

  • NitroTestingModule.create(AppModule, overrides={Token: instance_or_factory}). An instance is used as is; a class or plain function is called with no arguments (it may be async). The overrides are registered before the app is built, so the original provider is never constructed.
  • await harness.close() and async with. close() calls each provider's on_module_destroy hook (sync or async) once, in reverse creation order, then removes the overrides from the DI container so the next test starts clean. It is idempotent, and every hook runs even if one fails (the first error is raised afterwards). Overrides are also undone if create() itself fails.
  • MockLogger (has_log(level, message), clear()), create_mock_context(**overrides) and flush_tasks(), all in nitrostack.testing.
  • Optional pytest fixture nitro_client in nitrostack.testing.pytest_plugin, selected with @pytest.mark.nitro_app(module=Root) or a nitro_app fixture, and closed after the test. This replaces the old PR feat: expand security, background tasks, and dev tooling #4 fixture; the marker takes module= because pytest treats a lone class passed to a marker as a decoration.
  • README testing example. tests/test_readme_examples.py runs every README block marked <!-- readme-test -->.

Notes

  • The SDK had no provider shutdown hook, so this defines one: a method named on_module_destroy. Say if you prefer a different name.
  • McpApplication now keeps _module_instances (used by close()) and skips unittest.mock objects when it scans providers for tools. A MagicMock answers hasattr for everything, so without this it was registered as a tool.

Tests

tests/test_testing_module_overrides.py (15 tests) and tests/test_readme_examples.py (2): override used and original never built, factory forms, close once when called twice, async with, overrides restored (also after a failed create), failing hook, MagicMock overrides, logger, context, flush_tasks, the fixture, and no warnings with -W error. Full suite: 841 passed locally.

This branch has not been deployed

No deployments
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