nvim-neotest / neotest

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

No tests found #51

Closed armanschwarz closed 2 years ago

armanschwarz commented 2 years ago

I've attempted to follow the instructions to install neotest for running python unittest tests:

call plug#begin('~/.config/nvim/plugged')
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-treesitter/nvim-treesitter'
Plug 'antoinemadec/FixCursorHold.nvim'
Plug 'nvim-neotest/neotest'
Plug 'nvim-neotest/neotest-python'
call plug#end()

lua << EOF
require("neotest").setup({
  adapters = {
    require("neotest-python")({
        -- Extra arguments for nvim-dap configuration
        dap = { justMyCode = false },
        -- Command line arguments for runner
        -- Can also be a function to return dynamic values
        args = {"--log-level", "DEBUG"},
        -- Runner to use. Will use pytest if available by default.
        -- Can be a function to return dynamic value.
        runner = "unittest",

        -- Returns if a given file path is a test file.
        -- NB: This function is called a lot so don't perform any heavy tasks within it.
        is_test_file = function(file_path)
        end
    })
  }
})
EOF

Then I created this python test file:

import unittest

class MyTest(unittest.TestCase):
    def test_my_test_case(self) -> None:
        self.assertTrue(True)

Then I load vim, navigate to "my_test_case" and type :lua require("neotest").run.run() and I just get "No tests found".

rcarriga commented 2 years ago
    is_test_file = function(file_path)
   end

This is going to always say a file path is not a test file because of implicitly returning nil. Remove it from the config

armanschwarz commented 2 years ago

I tried using this instead:

require("neotest").setup({
  adapters = {
    require("neotest-python")({
        -- Extra arguments for nvim-dap configuration
        dap = { justMyCode = false },
        -- Command line arguments for runner
        -- Can also be a function to return dynamic values
        args = {"--log-level", "DEBUG"},
        -- Runner to use. Will use pytest if available by default.
        -- Can be a function to return dynamic value.
        runner = "unittest"
    })
  }
})

But I get the same result.

rcarriga commented 2 years ago

my_test_case is not a valid test name, please be sure the tests are actually runnable using the unittest cli.

armanschwarz commented 2 years ago

That was just a mve. I changed it to test_my_test_case and it's the same issue.

arman@docker-desktop: ~ $ python -m unittest mytest.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
rcarriga commented 2 years ago

OK can you update your plugins to the latest, retry and provide the log in vim.fn.stdpath("log") .. "neotest.log"?

armanschwarz commented 2 years ago
INFO | 2022-07-02T23:33:12Z+1000 | ....config/nvim/plugged/neotest/lua/neotest/client/init.lua:522 | Initialising client
INFO | 2022-07-02T23:33:12Z+1000 | ....config/nvim/plugged/neotest/lua/neotest/client/init.lua:589 | Initialisation finished in 0 seconds
rcarriga commented 2 years ago

Just saw your test file name is mytest.py, this is not an expected test file name. If you run python -m unittest it won't see your file. You should rename it to test_mytest.py. You can force it to be detected by neotest by changing the is_test_file option but it won't work when running directories

armanschwarz commented 2 years ago

Changed the name to test_mytest.py. No change.

arman@docker-desktop: ~/stuff $ python -m unittest
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
rcarriga commented 2 years ago

If you use this init.lua does it work? nvim --clean -u min.lua test_mytest.py

-- ignore default config and plugins
vim.opt.runtimepath:remove(vim.fn.expand("~/.config/nvim"))
vim.opt.packpath:remove(vim.fn.expand("~/.local/share/nvim/site"))
vim.opt.termguicolors = true

-- append test directory
local test_dir = "/tmp/nvim-config"
vim.opt.runtimepath:append(vim.fn.expand(test_dir))
vim.opt.packpath:append(vim.fn.expand(test_dir))

-- install packer
local install_path = test_dir .. "/pack/packer/start/packer.nvim"
local install_plugins = false

if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
  vim.cmd("!git clone https://github.com/wbthomason/packer.nvim " .. install_path)
  vim.cmd("packadd packer.nvim")
  install_plugins = true
end

local packer = require("packer")

packer.init({
  package_root = test_dir .. "/pack",
  compile_path = test_dir .. "/plugin/packer_compiled.lua",
})

packer.startup(function(use)
  -- Packer can manage itself
  use("wbthomason/packer.nvim")

  use("vim-test/vim-test")
  use({
    "nvim-neotest/neotest",
    requires = {
      "nvim-lua/plenary.nvim",
      "nvim-treesitter/nvim-treesitter",
      "antoinemadec/FixCursorHold.nvim",
      "nvim-neotest/neotest-python",
    },
    config = function()
      require("neotest").setup({
        adapters = {
          require("neotest-python")({
            dap = { justMyCode = false },
            args = { "--log-level", "DEBUG" },
            runner = "unittest",
          }),
        },
      })
    end,
  })

  if install_plugins then
    packer.sync()
  end
end)

vim.cmd([[
command! NeotestSummary lua require("neotest").summary.toggle()
command! NeotestFile lua require("neotest").run.run(vim.fn.expand("%"))
command! Neotest lua require("neotest").run.run(vim.fn.getcwd())
command! NeotestNearest lua require("neotest").run.run()
command! NeotestDebug lua require("neotest").run.run({ strategy = "dap" })
command! NeotestAttach lua require("neotest").run.attach()
]])
rcarriga commented 2 years ago

You'll also have to run TSInstall python and restart after installing plugins

armanschwarz commented 2 years ago

It seems to work with that init.lua file you provided:

  import unittest

  class MyTest(unittest.TestCase):
✔     def test_my_test_case(self) -> None:
          self.assertTrue(True)

I'm confused about what the difference is though, as I thought that the init.vim file I provided in the OP is basically the same thing as what you gave me, but somehow it seems to work. Any ideas?

armanschwarz commented 2 years ago

Ah so I tried again with my init.vim and it actually worked fine after running TSInstall python, so apparently that was the missing step.

armanschwarz commented 2 years ago

diff --git a/README.md b/README.md
index ba87818..39fd4a6 100644
--- a/README.md
+++ b/README.md
@@ -27,12 +27,12 @@ See `:h neotest` for details on neotest is designed and how to interact with it

 ## Installation

-Neotest uses [plenary.nvim](https://github.com/nvim-lua/plenary.nvim/).
-
-Most adapters will also require [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter).
-
 Neotest uses the `CursorHold` event which has issues in NeoVim: [see here](https://github.com/neovim/neovim/issues/12587) \
-It's recommended to use https://github.com/antoinemadec/FixCursorHold.nvim.
+It's recommended to use https://github.com/antoinemadec/FixCursorHold.nvim. Neotest uses \
+[plenary.nvim](https://github.com/nvim-lua/plenary.nvim/). Most adapters will also require \
+[nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) (and for the correct \
+[language parser](https://github.com/nvim-treesitter/nvim-treesitter#language-parsers) to be installed, e.g. by running\
+`:TSInstall all`).

 Install with your favourite package manager alongside nvim-dap