neotest-python icon indicating copy to clipboard operation
neotest-python copied to clipboard

No tests are being recognized within test files

Open a3ng7n opened this issue 1 year ago • 12 comments

It seems no tests within files are being discovered - when running file, or running nearest, or debugging nearest the result is "No tests found" from neotest. The test file, however, does show up in the summary window, but shows a "skipped" icon next to it when run. image

I created a fresh venv with python 3.12, and installed just pytest 8.3.3, and implemented their getting started:

# filename: test_sample.py
def func(x):
    return x + 1

def test_sample():
    assert func(3) == 5

class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")

Running pytest yields the following, expected, output: image

Here's my output of nvim -v:

% nvim -v
NVIM v0.11.0-dev
Build type: RelWithDebInfo
LuaJIT 2.1.1703358377
Run "nvim -V1 -v" for more info

And I'm running LazyVim - here's the lazy-lock.json lines for neotest:

  "neotest": { "branch": "master", "commit": "6d6ad113f56edc7c3f2a77a0836ea8c1b955ebea" },
  "neotest-python": { "branch": "master", "commit": "72603dfdbaad5695160268cb10531a14cc37236e" },

and the rest of my configuration is here: https://github.com/a3ng7n/nvim

a3ng7n avatar Sep 10 '24 20:09 a3ng7n

+1 getting this too after upgrading my nvim/neotest/neotest-pytest. Can't quite figure out which upgrade caused the regression

sergei1152 avatar Sep 13 '24 23:09 sergei1152

+1 getting this too after upgrading my nvim/neotest/neotest-pytest. Can't quite figure out which upgrade caused the regression

Yeah - I was suspecting this could be an issue in neotest proper as well, but just haven't spent enough time to figure out which.

a3ng7n avatar Sep 14 '24 00:09 a3ng7n

I had this bug on nightly build. when moving to v0.10.0 the bug was gone

saharwenrix avatar Sep 22 '24 14:09 saharwenrix

I had this bug on nightly build. when moving to v0.10.0 the bug was gone

is that v0.10.0 of neotest? or neotest-python?

if you're able to do any testing that might help track whether this is a bug in neotest or neotest-python

a3ng7n avatar Sep 24 '24 19:09 a3ng7n

is that v0.10.0

sounds like he was talking about neovim version. BTW I'm also facing this issue on neovim 0.10.2

NOTE: After reinstalled properly it works, steps:

  1. :LazyExtra
  2. check lang-python and neotest
+  "neotest": { "branch": "master", "commit": "6d3d22cdad49999ef774ebe1bc250a4994038964" },
+  "neotest-python": { "branch": "master", "commit": "a2861ab3c9a0bf75a56b11835c2bfc8270f5be7e" },

parisni avatar Nov 30 '24 14:11 parisni

For me, this issue had to do with an outdated version of plenary.nvim (symptoms exactly as described). The latest release of plenary is from 2023. Plenary is used to detect filetypes in neotest, which returned nil on this version. Installing the latest git version of plenary.nvim fixed the issue for me.

Quitlox avatar Dec 18 '24 13:12 Quitlox

Same issue on 0.10.2, all my Neotest adapters work fine, except Python for some reason.

But what is really funny, if I navigate into a Rust file with a test in it, and run the tests, then when I go back into the python file with tests and run them - neotest runs them 😅. If I make any test to fail, I can see python adapter actually reporting the test failure.

Otherwise I always see "No tests found" message.

olisikh avatar Jan 02 '25 22:01 olisikh

I think I found the root cause of this issue. If you just add one of the following files into the root folder, even an empty one:

"pyproject.toml", "setup.cfg", "mypy.ini", "pytest.ini", "setup.py"

which I had none of, then tests should be discovered and you should be able to run them. At least it started working for me.

The issue was in Adapter.get_root function, neotest was looking for any of these files and if none of them are found, then it considers the folder to be not a neotest-enabled workspace.

olisikh avatar Jan 03 '25 00:01 olisikh

Getting the same issue but I already have a pyproject.toml in my root dir.

bavjackson avatar Mar 15 '25 17:03 bavjackson

I think I found the root cause of this issue. If you just add one of the following files into the root folder, even an empty one:

"pyproject.toml", "setup.cfg", "mypy.ini", "pytest.ini", "setup.py"

which I had none of, then tests should be discovered and you should be able to run them. At least it started working for me.

The issue was in Adapter.get_root function, neotest was looking for any of these files and if none of them are found, then it considers the folder to be not a neotest-enabled workspace.

I think it would be sensible to add .git to that list 😅

juliancoffee avatar May 06 '25 23:05 juliancoffee

Hi! I have a similar issue: test_*.py files are recognized, but not their contents.

Versions of everything

pytest 8.3.5 NVIM v0.11.1

Part of lazy-lock.json that might be helpful (everything is latest I believe):

{
  "FixCursorHold.nvim": { "branch": "master", "commit": "1900f89dc17c603eec29960f57c00bd9ae696495" },
  "neotest": { "branch": "master", "commit": "862afb2a2219d9ca565f67416fb7003cc0f22c4f" },
  "neotest-python": { "branch": "master", "commit": "a2861ab3c9a0bf75a56b11835c2bfc8270f5be7e" },
  "nvim-nio": { "branch": "master", "commit": "21f5324bfac14e22ba26553caf69ec76ae8a7662" },
  "nvim-treesitter": { "branch": "master", "commit": "066fd6505377e3fd4aa219e61ce94c2b8bdb0b79" },
  "plenary.nvim": { "branch": "master", "commit": "857c5ac632080dba10aae49dba902ce3abf91b35" }
}

Neotest config

return {
    "nvim-neotest/neotest",
    dependencies = {
        "nvim-neotest/nvim-nio",
        "nvim-lua/plenary.nvim",
        "antoinemadec/FixCursorHold.nvim",
        "nvim-treesitter/nvim-treesitter",
        "nvim-neotest/neotest-python",
    },
    config = function()
        require("neotest").setup({
            output = { open_on_run = true },
            adapters = {
                require("neotest-python")({
                    log_level = vim.log.levels.DEBUG,
                    runner = "pytest",
                }),
            }
        })
    end,
}

pytest is installed via pipx, running it from terminal works perfectly fine.

Sample project

.
├── main.py (empty)
├── pyproject.py (empty)
└── tests
    └── test_dummy.py

Contents of tests/test_dummy.py:

def test_dummy() -> None:
    assert 1 == 2

Behavior

Neotest summary shows the tests/test_dummy.py file, but without any contents: Image

Running all tests in the buffer turns question mark to skipped mark: Image

kompoth avatar May 19 '25 16:05 kompoth

If anyone is still reading this issue, I think I've figured this one out.

Docs says:

Requires nvim-treesitter and the parser for python.

That was basically the case for me. It was a fresh nvim install and there was no treesitter for python.

Probably would be nice to add treesitter dependency to the docs example? I have it like this now:

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


        require("neotest").setup({
            output = { open_on_run = true },
            adapters = {
                require("neotest-python")({
                    log_level = vim.log.levels.DEBUG,
                    runner = "pytest",
                    python = ".venv/bin/python",
                }),
            }
        })
    end,
}

kompoth avatar May 26 '25 14:05 kompoth