pytest-dev / pytest-dev/pytest

Generalize pytest_assertrepr_compare() hook so it works with any assert

Open
#5,535 10 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi All,

I am new to pytest. I need help with assertion hook.
There is pytest_assertrepr_compare() hook for assertion expression comparison.
However the hook will work only if assert statement has both left and right expressions along with operands.
Example

  1. assert 1 == 2
  2. assert i == j
  3. assert 'a' not in 'assert':

My use case is - Every time an assertion failure happens, I need a hook to enter in and manage few test specific variables defined inside my conftest.
But since the hook requires an operator, it will not work for below assert statements.

  1. assert i
  2. assert not j
    etc.

Is there a solution to this, that allows me to enter a hook on every assertion failure ?

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

Start by reading the pytest_assertrepr_compare() hook and the assertion-handling path described in the issue, along with how conftest variables are accessed. Determine whether a general assertion-failure hook can cover bare and negated assertions without changing comparison behavior. Done should include a documented, tested way to handle every assertion failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.