approvals / approvals/ApprovalTests.Python

Support `__test__` attribute in PyTest test discovery

Open
#161 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
212
Forks
56
Avg merge
1m
Merged PRs (30d)
1

Description

A recent release included support for alternative (i.e. non-"test") test prefixes.

Pytest, also uses a custom __test__ dunder when discovering tests: python.py#L349. I.e. any function with the attribute __test__ = True is considered a test regardless of its prefix. Currently, this library does not support this dunder.

Would it be possible to support the __test__ dunder in the recently added is_pytest_test method?

I am happy to work on this unless someone else sees a quick fix.

Suggested Fix

An additional test in the StackFrameNamer class for the __test__ dunder could be something like the following:

@staticmethod
def _is_marked_with_test_dunder(
      method_name: str,
      frame_globals: dict[str, "Any"],
) -> bool:
    function = frame_globals.get(method_name)
    return (
          function is not None
          and hasattr(function, "__test__")
          and getattr(function, "__test__") is True  # i.e. True not truthy
    )

@staticmethod
def is_pytest_test(frame: FrameInfo) -> bool:
    method_name = frame[3]
    frame_globals = frame.frame.f_globals
    patterns = PytestConfig.test_naming_patterns
    return (
          StackFrameNamer._is_match_for_pytest(method_name, patterns)
          or StackFrameNamer._is_marked_with_test_dunder(
                method_name,
                frame_globals,
          )
    )

Here, the function name is looked up in the frame globals, and if found, it checks for the __test__ dunder. I would need to refresh my memory on Python's FrameInfo to check whether this is the best way of doing this.

Contributor guide

No contributing guide indexed for this repository

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 in approvaltests/namer/stack_frame_namer.py at StackFrameNamer.is_pytest_test and review the existing discovery tests for that class. Add coverage for functions whose test attribute is exactly True, then run the relevant test suite and confirm those functions are recognized regardless of their name prefix.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.