Skip to content

Add module (__name__) to pytest_assertion_pass hook #8263

@Sup3rGeo

Description

@Sup3rGeo

What's the problem this feature will solve?

The pytest_assertion_pass hook provides the test item and lineno, but sometimes the assertion line comes from an utility function/fixture in a different module and not from the test body itself.

Also it would be nice to be able to log from the logger of the module where the assertion code is, as if the logging call was hardcoded together with the assertion.

Describe the solution you'd like

Add a new module argument to pytest_assertion_pass.

Usage example:

def pytest_assertion_pass(item, module, lineno, orig, expl):
     logging.getLogger(module).info(f"Assertion passed for {orig}")

This could be relatively easily accomplished by adding an argument in the rewrite.py module:

            hook_call_pass = ast.Expr(
                self.helper(
                    "_call_assertion_pass",
                    ast.Name("__name__", ast.Load())  # <- module name 
                    ast.Num(assert_.lineno),
                    ast.Str(orig),
                    fmt_pass,
                )
            )

Alternative Solutions

Additional context

If you feel this is a feature worth having and the solution is fine, I would be more than happy to open a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: loggingrelated to the logging builtin plugintopic: rewriterelated to the assertion rewrite mechanismtype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions