Skip to content

Conversation

miketheman
Copy link
Member

See https://pytest-mock.readthedocs.io/

Example of using mocker fixture to add a mocker.spy for a single function and asserting its calls in the test.

See https://pytest-mock.readthedocs.io/

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Most of these are not necessary, but used to demonstrate the utility

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@miketheman miketheman added the testing Test infrastructure and individual tests label Apr 22, 2025
return account_services.NullDomainStatusService()
def domain_status_service(mocker):
service = account_services.NullDomainStatusService()
mocker.spy(service, "get_domain_status")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the fixture sets up the tracking internally, and doesn't change what's returned by calling the fixture, only adds some methods.

If I try to spy on a method that doesn't exist, an AttributeError is raised, so that protects us from spying on things that don't exist, whereas a call_recorder on a created stub offers no such protection.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@miketheman
Copy link
Member Author

Closing, as it helped illustrate the point. The inclusion of the helper is in #18014

@miketheman miketheman closed this Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Test infrastructure and individual tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant