nvim-neotest / neotest

An extensible framework for interacting with tests within NeoVim.
MIT License
2.35k stars 115 forks source link

Extra arguments are not adapter specific #81

Closed christoph-blessing closed 2 years ago

christoph-blessing commented 2 years ago

I have the following command bound to a key to run all tests in my project:

require('neotest').run.run(vim.fn.getcwd())

This works fine but I have some slow tests in my test suite and I would like to exclude them from running when using the command mentioned above. Fortunately my test runner (pytest) lets me apply custom marks to tests and then I can exclude marked tests with a command line argument. Now I want to adapt the command above to pass the appropriate command line argument to pytest. One option would be to do this:

require('neotest').run.run({vim.fn.getcwd(), extra_args = {"-m", "'not slow'"}})

This works but is not ideal because other test runners beside pytest will not understand extra_args and (presumably) error out. What is the recommended way to deal with this?

One solution I could see is to write a custom function that checks if the test runner to be used is pytest and then passes the appropriate extra_args. If that is the best solution I would appreciate it if you could let me know how to determine the test runner that is about to be used.

rcarriga commented 2 years ago

You could do something like this

  local neotest = require("neotest")
  local skip_slow = false
  neotest.setup({
    adapters = {
      require("neotest-python")({
        args = function ()
          if skip_slow then
            return {"-m", "'not slow'"}
          end
          return {}
        end
      }),
    },
  })

  vim.api.nvim_create_user_command("NeotestSuite", function (opts)
    local parsed_value = ...-- Parse opts to decide what to set
    skip_slow = parsed_value
    neotest.run.run({ suite = true })
  end, {...})
christoph-blessing commented 2 years ago

Cool, that does exactly what I want. Thanks a lot and keep up the great work with this plugin!