pytest-dev / pytest-dev/pytest

Exclude variable from showlocals output

Open
#8,974 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: reporting 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?

I have a test which uses a large dictionary of words. With showlocals on (which is generally really useful and I have it globally enabled) this huge dictionary gets printed for any test failure.

This is distinct from the case of credentials, as my data is not private and I don't care if something prints it, I just don't want --showlocals to print it.

Describe the solution you'd like

A way of excluding variables from output, attached to either the test function via annotation, or a @pytest.fixture (in my case the dictionary is a fixture)

There must be other cases where large test data objects make the test output more difficult to read.

Alternative Solutions

Can disable showlocals and go back to normal debugging.

Can wrap the object in a class with a custom __repr__ and __str__ implementation. However this is clumsy, has too wide a scope, and is error prone (for example not implementing __eq__ may lead to test failures for string-like objects).

Additional context

I'm mostly looking for workarounds, I understand this is a niche request and might not be productive to be added into pytest itself.

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 tracing pytest's --showlocals output handling and reviewing how fixture and test-function values are represented. Define an exclusion mechanism that works for large non-sensitive objects without changing their behavior, and verify that excluded variables no longer appear while other local values remain visible.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.