Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run decorated test when cursor is on the decorators #31

Closed
OddBloke opened this issue Nov 7, 2022 · 3 comments · Fixed by #34
Closed

Run decorated test when cursor is on the decorators #31

OddBloke opened this issue Nov 7, 2022 · 3 comments · Fixed by #34

Comments

@OddBloke
Copy link
Contributor

OddBloke commented Nov 7, 2022

Currently, with a test file that looks like:

def test_one():
    pass

@pytest.mark.parametrize(...)
def test_thing(...):
   pass

invoking neotest.run.run with the cursor on the parametrize line results in test_one being run.

I would prefer for test_thing to be run: that's the test whose definition I'm editing.

@OddBloke
Copy link
Contributor Author

OddBloke commented Nov 7, 2022

I've never touched treesitter queries before, but my first blind guess for functions is a decent approximation:

--- a/lua/neotest-python/init.lua
+++ b/lua/neotest-python/init.lua
@@ -91,6 +91,12 @@ function PythonNeotestAdapter.discover_positions(path)
     (class_definition
      name: (identifier) @namespace.name)
      @namespace.definition
+
+    (decorated_definition
+      ((function_definition
+        name: (identifier) @test.name)
+        (#match? @test.name "^test")))
+        @test.definition
   ]]
   local root = PythonNeotestAdapter.root(path)
   local python = get_python(root)

This annotates the first decorator line, rather than the def line. I tried doing the same copy/paste trick with the class definition and that didn't work.

@rcarriga
Copy link
Collaborator

rcarriga commented Nov 8, 2022

Actually have been meaning to add support for parsing the parameterized tests as well, just haven't had a chance.

I put together a PR for neotest-dotnet that did something very similar to what's needed here Issafalcon/neotest-dotnet#20. The core of the change is discussed here nvim-neotest/neotest#24 (reply in thread) where the parsing is customised to run queries after the initial parse, so the initial queries remain the same and then we traverse the tree to check if there are parameters.

If you want to have a go at it, happy to help but if not, I'll get around to it myself at some stage 😄

OddBloke added a commit to OddBloke/neotest-python that referenced this issue Nov 8, 2022
This means that `neotest.run.run` when the cursor is on a test's
decorators will run the decorated test (instead of running the previous
test in the file, the current behaviour).

Fixes: nvim-neotest#31
@OddBloke
Copy link
Contributor Author

OddBloke commented Nov 8, 2022

This is actually a more general problem than just parametrized tests: @mock.patch decorators, for example, are included in the prior test's definition. Doing more with test parameterization would certainly be cool though!

I figured out my classes problem: namespaces nest, and my first copy/paste rule meant that two namespaces were being annotated for decorated classes: the definition itself and the decorated definition, with the former nesting inside the latter. I've pushed up #34 which has a proper approach to this.

rcarriga pushed a commit that referenced this issue Nov 10, 2022
This means that `neotest.run.run` when the cursor is on a test's
decorators will run the decorated test (instead of running the previous
test in the file, the current behaviour).

Fixes: #31
JBSK8NC pushed a commit to JBSK8NC/neotest-python that referenced this issue Sep 9, 2023
…test#34)

This means that `neotest.run.run` when the cursor is on a test's
decorators will run the decorated test (instead of running the previous
test in the file, the current behaviour).

Fixes: nvim-neotest#31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants