Skip to content

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

@sruizr

Description

@sruizr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions