nvim-neotest / nvim-neotest/neotest-python

neotest-python fails to discover deeply nested pytest-describe tests

Open
#113 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
192
Forks
60
PR merge metrics
No merged PRs in 30d

Description

Issue: neotest-python fails to discover deeply nested pytest-describe tests

Summary

neotest-python cannot discover or run pytest-describe tests organized with container functions at arbitrary nesting depths. Tests are marked as not found, and the error message shows only the leaf test function name without the full container hierarchy.

Description

When using pytest-describe with nested container functions (describe, context, when, etc.), neotest-python fails to build the complete test path needed by pytest.

Example Test Structure
def describe_header():
    """Header structure for all ASDUs."""
    def context_asdu_address_validation():
        """Validation rules for asdu_address field."""
        def when_asdu_address_is_below_minimum():
            """asdu_address < 1 violates the minimum constraint."""
            def then_it_raises_asdu_validation_error():
                with pytest.raises(AsduValidationError):
                    Header(
                        type_id=VALID_TYPE_ID,
                        cause=VALID_CAUSE,
                        asdu_address=BELOW_MINIMUM_ASDU_ADDRESS,
                    ) 
Error Message
ERROR: not found: /home/.../tests/test_shared.py::then_it_raises_asdu_validation_error
(no match in any of [<Module test_shared.py>])
Expected Behavior

pytest can find and run the test when given the full path:

$ pytest tests/test_shared.py::describe_header::context_asdu_address_validation::when_asdu_address_is_below_minimum::then_it_raises_asdu_validation_error
# Test runs successfully
Actual Behavior

neotest-python only tries to run with the leaf function name, missing all parent container functions.

Root Cause

  1. Limited treesitter query: neotest-python's treesitter query only recognizes test functions (test_*, it_*), not container functions
  2. No describe_prefixes support: The treesitter query doesn't recognize pytest-describe's describe_prefixes configuration
  3. Shallow nesting only: The query pattern only handles 2 levels of nesting maximum

Environment

  • neotest-python: Latest version
  • pytest-describe: 3.1.0+
  • nvim-treesitter: Latest
  • Python: 3.x

Configuration

pyproject.toml:

[tool.pytest.ini_options]
python_functions = ["test_*", "then_*"]
python_classes = ["Test*", "Describe*", "When*"]
describe_prefixes = ["describe", "context", "when", "given", "scenario", "requirement"]

Steps to Reproduce

  1. Create a pytest test file with nested pytest-describe container functions (3+ levels)
  2. Open in Neovim with neotest and neotest-python
  3. Try to run a nested test function
  4. Observe "not found" error

Solution Proposal

The treesitter query generation should:

  1. Extract describe_prefixes from pytest configuration
  2. Generate query rules for container functions (creating namespaces)
  3. Generate query rules for test functions
  4. Support unlimited nesting depth
    This would allow neotest-python to correctly build the full test path hierarchy that pytest expects.

Related Files

  • lua/neotest-python/base.lua - Treesitter query generation
  • neotest_python/pytest.py - pytest configuration extraction

Impact

This affects anyone using pytest-describe with nesting deeper than 1 level. The plugin should support arbitrary nesting depths as pytest-describe itself does.

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

Read lua/neotest-python/base.lua and neotest_python/pytest.py first, tracing treesitter query generation and pytest configuration extraction. Reproduce the nested pytest-describe example and compare the generated test path with pytest's full path. Done means nested containers at three or more levels are discovered and runnable with their complete hierarchy.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, neovim, python
Domain
testing-qa, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.