nvim-neotest / neotest-python

MIT License
115 stars 34 forks source link

Unitests not found if you are in a django project #67

Closed antoniogamizbadger closed 4 months ago

antoniogamizbadger commented 4 months ago

Not sure why this is happening, but I'm running this in a Django project. I'm using the following shortcut:

vim.keymap.set("n", "<leader>df", "<cmd>lua require('neotest').run.run({vim.fn.expand('%')})<cr>", {})

If I run in a file inheriting from Django test case, like this:

from unittest.mock import MagicMock, patch
from django.test import TestCase
from common.jwt.token_auth import TokenAuth

class TokenAuthCase(TestCase):
    @patch("time.time", MagicMock(return_value=12345))
    def test_given_content_when_building_a_token_then_it_is_correctly_created(self):
        ...

Then the tests are found correctly. Nonetheless, if now I run the same shortcut in a test file where unittest testcase is used, I always get no tests found message:

import unittest
from unittest.mock import MagicMock, patch

class DataSyncServiceTestCase(unittest.TestCase):
     def test_whatever(self):
         ...

So not sure what it's going on. I'm new to neovim, so I may have something incorrect in my config. I tried in a pure unitest project, where django is not involved, and it was working correctly.

This is my neotest config (using lazy.vim), in case it helps:

return {
    {
        "nvim-neotest/neotest",
        dependencies = {
            "nvim-lua/plenary.nvim",
            "antoinemadec/FixCursorHold.nvim",
            "nvim-treesitter/nvim-treesitter",
        },
    },
    {
        "nvim-neotest/neotest-python",
        dependencies = {
            "nvim-neotest/neotest",
        },
        config = function()
            require("neotest").setup({
                adapters = {
                    require("neotest-python"),
                },
            })

            -- run nearest test
            vim.keymap.set("n", "<leader>dm", "<cmd>lua require('neotest').run.run()<cr>", {})

            -- debug nearest test
            vim.keymap.set("n", "<leader>dM", "<cmd>lua require('neotest').run.run({strategy = 'dap'})<cr>", {})

            -- run whole file
            vim.keymap.set("n", "<leader>df", "<cmd>lua require('neotest').run.run({vim.fn.expand('%')})<cr>", {})

            -- debug whole file
            vim.keymap.set(
                "n",
                "<leader>dF",
                "<cmd>lua require('neotest').run.run({vim.fn.expand('%'), strategy = 'dap'})<cr>",
                {}
            )

            vim.keymap.set("n", "<leader>do", "<cmd>lua require('neotest').output.open({ enter = true })<cr>", {})

            -- toggle summary
            vim.keymap.set("n", "<leader>dS", "<cmd>lua require('neotest').summary.toggle()<cr>", {})

            -- open output panel
            -- vim.keymap.set("n", "<leader>to", "<cmd>lua require('neotest').output_panel.open()<cr>", {expr = true})
            vim.keymap.set("n", "<leader>to", function()
                require("neotest").summary.clear()
                require("neotest").summary.output_panel.open()
            end, { expr = true })
        end,
    },
}
antoniogamizbadger commented 4 months ago

I have seen this in the docs -> https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings . There is a django flag, but that only enables support to debug html templates, so it should not affect this behaviour.

antoniogamizbadger commented 4 months ago

I also tried checking if the test file is recognised (see https://github.com/nvim-neotest/neotest-python/issues/52#issuecomment-1676507664), and it is.

antoniogamizbadger commented 4 months ago

Hum, instead of using unittest.TestCase, I have used django.test.TestCase and now the tests are found. Not sure why though. @afrischk I have seend you developed the support for django, so I'm pinging to see if may be you know something about it?

antoniogamizbadger commented 4 months ago

So it seems you can use :echo stdpath("logs") to get the directory where nvim logs are. In there, you can check neotest.log. When I took a look there, I found the following:

image

It seems the mounted volume of the postgres image in my project, has root permissions. So the test finder was looking in that directory and it was triggering an error. What I don't get is why is looking there.

This solves the issue and now the tests are fine. Leaving open in case we can discuss a little bit why that directory is being searched?

afrischk commented 4 months ago

Hey @antoniogamizbadger I noticed that you have not configured the django runner. It should be detected by default though:

https://github.com/nvim-neotest/neotest-python/blob/2e83d2bc00acbcc1fd529dbf0a0e677cabfe6b50/lua/neotest-python/init.lua#L68

require("neotest").setup({
  adapters = {
    require("neotest-python")({
          runner = "django"
    }),
  },
})

The django test runner works with unittest.TestCases and with django.test.TestCases that should be no problem.

Do you have __init__.py files in the test packages? If not, tests will not be found.

You could also use:

  is_test_file = function(filename)
      return filename:match("test_.+%.py$")
  end,

Maybe this helps finding the tests.

Please check if you have configured a DB in the django settings as the default test runner from django tries to setup the db. Maybe this is an issue here? See: https://github.com/django/django/blob/cfecac27dedc27b5e7f098273787994f49d40037/django/test/runner.py#L1061

rcarriga commented 4 months ago

The directory is being searched because neotest-python doesn't know where to look for tests. You can configure the filtering with the discovery.filter_dir option for neotest.setup but the latest commit of neotest actually already works around errors when reading directories anyway so if you update you should see no problems :smile: