nvim-neotest / neotest-python

MIT License
137 stars 40 forks source link

Running all tests under submobule in test summary panel #45

Closed Willem-J-an closed 1 year ago

Willem-J-an commented 1 year ago

I raised this for neotest but I found the issue originates in this repo.

I provide my arguments to neotest-python like so:

require("neotest-python")({
  args = {"-p", "*_test.py"}
})

These arguments are passed through all the way to the UnittestNeotestAdapter, which happily ignores them.

# These will be the args up to that point:
args = Namespace(
  runner='unittest',
   results_file='/tmp/nvim.user/yzlLad/0',
   stream_file='/tmp/nvim.user/yzlLad/1',
   args=['-p', '*_test.py', '/home/user/code/mymodule']
 )

Processed by the adapter

# We only get a single case ID as the argument
argv = sys.argv[0:1] + self.id_to_unittest_args(args[-1])

This prevents my test file pattern from reaching the TestProgram, where an incorrect pattern is the default: test*.py

And so it was, that not a single test was tested :D Ran 0 tests in 0.000s

rcarriga commented 1 year ago

Thanks for figuring out the issue :smile: I've adjusted the argument parsing to correctly pass user args. Please try with the latest commit

Willem-J-an commented 1 year ago

Hi, Unfortunately it is not working yet for me.

Prepended the command now becomes this:

['neotest-python', '-p', '*_test.py', 'discover', '-s', '/home/user/code/module']

This is invalid because the pattern must come after discover. If the args are appended, it does work for me.

argv = ["neotest-python"] +  self.id_to_unittest_args(args[-1]) + args[:-1]
rcarriga commented 1 year ago

Ah yep sorry about that, please try latest!

Willem-J-an commented 1 year ago

It works now for a directory with multiple tests, but not when running a single test. neotest-python: error: unrecognized arguments: -p *_test.py I guess the code path with multiple tests uses discover, which accepts this parameter, but the code path for a single test does not use discover, and does not accept this parameter.

rcarriga commented 1 year ago

OK it'll now pass the args in the correct order :sweat_smile:

Willem-J-an commented 1 year ago

It doesn't seem quite there yet for me unfortunately. From the summary panel I can:

What no longer works is:

From a testfile I can sucesfully expand and run all, I suppose this runs the entire file, which also works from the summary panel. I can not run a singular test with neotest.run.run().

The reason it does not work seems to be my discover arguments are passed to a non discover command when running a single testcase. usage: neotest-python [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [-k TESTNAMEPATTERNS] [tests ...] neotest-python: error: unrecognized arguments: -p

rcarriga commented 1 year ago

neotest-python doesn't parse the args you pass, you have to be sure to pass the correct arguments. The args option can be a function that receives two arguments: runner ("unittest" or "pytest") and position which is a neotest.Position so you can do something like this:

require("neotest-python")({
  args = function (_, position)
    if position.type == "test" or position.type == "namespace" then
      return {}
    end
    return {"-p", "*_test.py"}
  end
})
Willem-J-an commented 1 year ago

Ah great, that makes sense now! I would recommend to include this in the docs somewhere? Could be helpful for the next person! :smile:

Thanks a lot for the help