nvim-neotest / neotest-python

MIT License
126 stars 38 forks source link

fix: unittest adapter can run specific tests/groups/files #2

Closed stevearc closed 2 years ago

stevearc commented 2 years ago

Summary

Neotest was unable to run a single test in a file. Actually, was always running all files discoverable by the default test runner.

This is due to how the args were getting passed in. The argv argument for unittest.main() expects a format that is identical to sys.argv, i.e. the first element is the program name and the remainder are the program args. We were passing the test ID <testfile>.py::TestClass::test_fn as the only argument, so it was being used as the program name and unittest was running the default behavior, which is to discover and run all tests relative to the current directory.

The fix is to pass in the actual program name as the first arg, and to convert the test IDs to be the format expected by unittest. From python -m unittest -h

Examples:
  python -m unittest test_module               - run tests from test_module
  python -m unittest module.TestClass          - run tests from module.TestClass
  python -m unittest module.Class.test_method  - run specified test method
  python -m unittest path/to/test_file.py      - run tests from test_file.py

Test Plan

before: neotest before

after:

neotest after

I also confirmed that testing the entire file works as before :lua require('neotest').run.run(vim.api.nvim_buf_get_name(0)) neotest file

stevearc commented 2 years ago

Second commit fixed an issue with running tests specified by a directory (it would run 0 tests, and they would appear as stuck running). It makes the assumption that the script is only passed a single ID arg, which I think is the case because of https://github.com/rcarriga/neotest-python/blob/e040840e6550a55cc32d21cc56f6a8db5041fb94/lua/neotest-python/init.lua#L110-L112

rcarriga commented 2 years ago

Thanks for the PR! :grin: Just a couple of small comments and a question

stevearc commented 2 years ago

@rcarriga What are the comments and question? I think Github may have swallowed your review 😄

rcarriga commented 2 years ago

It would help if I hit submit :sweat_smile:

stevearc commented 2 years ago

Comments addressed