pytest-dev / pytest-dev/pytest

Add module (__name__) to pytest_assertion_pass hook

Open
#8,263 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plugin: logging topic: rewrite type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The requested hook change is centered in rewrite.py, where _call_assertion_pass is assembled; start there and trace how pytest_assertion_pass receives its arguments. Check the existing assertion-pass behavior before determining the corresponding coverage. Done means the hook receives the module name for assertions rewritten from another module while existing arguments continue to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.