pytest-dev / pytest-dev/pytest

An easier way to have test cases in built-in functions

Open
#11,388 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'd like to suggest that calls to inspect.isfunction are replaced with inspect.isroutine.

What's the problem this feature will solve?

I'm trying to test some native code functions I have written in Cython. Those are either coming from .pxd files or are cdef functions in .pyx files. These are not visible to Python code. They can only be called from Cython functions (which are built-in functions).

Describe the solution you'd like

I'd like to add a conftest.py file which defines in its pytest_collect_file how to load a Cython test module and then write test_* functions in Cython which call the said native functions.

Alternative Solutions

It is currently impossible to have a test_* function be a built-in function. It fails the check for inspect.isfunction defined in src/_pytest/python.py. As a workaround I copy-paste the entire pytest_pycollect_makeitem and replace its isfunction checks with isroutine checks. This is enough of a workaround to get my test cases loaded.

Additional context

It's unfortunate that I have to copy-paste library code. This goes against the whole idea of having hooks. One solution is to add new hooks. Another is to just allow a wider range of callables to be detected as test functions.

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 in src/_pytest/python.py around the inspect.isfunction check at line 247 and read how pytest collects test functions. Verify how Cython-built-in functions are represented, then confirm that replacing the narrow check with inspect.isroutine allows them to be collected without regressing ordinary test discovery.

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.