nvim-neotest / nvim-neotest/neotest-python

Running Neotest on a unittest folder fails when using `unittest` runner

Open
#69 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
192
Forks
60
PR merge metrics
No merged PRs in 30d

Description

Demo

https://github.com/nvim-neotest/neotest-python/assets/10103049/a3246a59-cf7c-48f2-99c9-aad4f9459d64

Description

Reproduction Details

  • Load Neovim with the reproduction file
cd neotest_reproduction
PYTHONPATH=$PWD:$PYTHONPATH nvim -u reproduction_neotest_test.lua tests/test_something.py
  • Call :Neotest summary
  • In the Neotest right-hand window, press r on the tests folder
    • With the unittest runner enabled in reproduction_neotest_test.lua, it fails
    • With the pytest runner enabled in reproduction_neotest_test.lua, it succeeds
Reproduction Files

(You don't need this file, it's in the tar.gz. But pasted it here so people can see

reproduction.lua
-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

local lazypath = root .. "/plugins/lazy.nvim"

if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end

vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    {
        "nvim-neotest/neotest",
        config = function()
            require("neotest").setup{
              adapters = {
                require("neotest-python")({
                    -- runner = "pytest",
                    runner = "unittest",
                })
              }
            }
        end,
        dependencies = {
            "antoinemadec/FixCursorHold.nvim",
            "nvim-lua/plenary.nvim",
            "nvim-treesitter/nvim-treesitter",

            "nvim-neotest/neotest-python",
        },
    },

    {
        "neovim/nvim-lspconfig",
        config = function()
            local lspconfig = require("lspconfig")
            local capabilities = vim.lsp.protocol.make_client_capabilities()

            lspconfig.jedi_language_server.setup { capabilities = capabilities }
        end,
    },
    {
        "nvim-treesitter/nvim-treesitter",
        build = ":TSUpdate",
        config = function()
            require('nvim-treesitter.configs').setup{
                ensure_installed = { 'python', 'query' },
            }
        end,
    },
}

require("lazy").setup(plugins, { root = root .. "/plugins" })

Expected

Running on the tests folder should still run as expected (or, if there was a subfolder, it should also run as expected). This is working for pytest but not for unittest.

The likely reason for the failure is because the test runner isn't sourcing tests/__init__.py when Neotest runs on a directory but tests/__init__.py is sourced when you run on a file. When I need to run unittests based on a directory, I typically do python -m unittest discover --start-directory tests/ or python -m unittest discover --start-directory tests/some_subfolder/ to make sure all __init__.py files are sourced correctly. Could neotest-python be adjusted to make sure tests run consistently regardless of the test's starting point?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction_neotest_test.lua configuration and the unittest behavior for tests/test_something.py; compare running the file with running the tests directory. Inspect how the unittest runner handles tests/init.py and directory discovery, then rerun :Neotest summary on the tests folder and a subfolder to verify consistent execution.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, neovim, python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.