-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom test function names are not handled #65
Comments
Do you have an existing workaround while this is still being looked at? I don't know how to request a bump for your pr. |
There are two things you can do
|
Hi @nicos68 I just had the same issue but my use case is having tests in the same implementation file. I implemented the is_test_fille function to get the file pattern from I'm leaving my implementation below... I had to install toml using luarocks which was a challenge, but nvm I'm just starting with neovim. I also learn from here that we need to avoid using vim.fn.glob2regpat since it's not coroutine safe, so I had to use :match. Summarizing:
Also to be able to use toml library I had to install it for lua 5.1 (since neovim runs in that version).
It'll be good to have this functionality by default since it's not straighforward to make it work. local toml = require("toml")
local nio = require("nio")
local cached_config = nil
local function is_match(base_name, pattern)
local lua_pattern = pattern:gsub("%%", "%%%%"):gsub("%*", ".*"):gsub("%?", ".") -- Convert glob to Lua pattern
return base_name:match(lua_pattern) ~= nil
end
local function get_config()
if cached_config then
return cached_config
end
local file, err = nio.file.open(vim.fn.getcwd() .. '/' .. 'pyproject.toml')
if not file or err then
return nil
end
local content = file.read(nil, 0)
cached_config = toml.decode(content)
return cached_config
end
local function get_pytest_config()
local config = get_config()
if config and config.tool and config.tool.pytest and config.tool.pytest.ini_options then
return config.tool.pytest.ini_options
end
return nil
end
-- @async
local function is_test_file(file_path)
if not vim.endswith(file_path, ".py") then
return false
end
local pytest_config = get_pytest_config()
local test_patterns = { "test_*.py", "*_test.py" } -- Default patterns
-- If pytest configuration is found, override test_patterns
if pytest_config and pytest_config.python_files then
-- Handle both single string and list of patterns
if type(pytest_config.python_files) == "string" then
test_patterns = { pytest_config.python_files }
elseif type(pytest_config.python_files) == "table" then
test_patterns = pytest_config.python_files
end
end
-- Check if the file matches any test pattern
local base_name = file_path:match("^.+/(.+)$")
for _, pattern in ipairs(test_patterns) do
if is_match(base_name, pattern) then
return true
end
end
return false
end
return {
"nvim-neotest/neotest",
version = "~5.6.0",
dependencies = {
"nvim-neotest/nvim-nio",
"nvim-lua/plenary.nvim",
"antoinemadec/FixCursorHold.nvim",
"nvim-treesitter/nvim-treesitter",
"nvim-neotest/neotest-python",
"nvim-neotest/neotest-plenary",
"nvim-neotest/neotest-vim-test",
"nvim-treesitter/nvim-treesitter",
"LebJe/toml.lua",
},
config = function ()
require("neotest").setup {
adapters = {
require("neotest-python") {
runner = "pytest",
dap = { justMyCode = false },
args = { "--log-level", "DEBUG" },
is_test_file = function (test_file)
nio.scheduler()
return is_test_file(test_file)
end
},
-- ...
},
}
-- ...
} |
I am working on a project for which we have cutomized the python_functions option value (using the
pyproject.toml
file). In this case neotest-python does not detect the tests.I tried fiddling with the treesitter query and it does solve the issue.
The text was updated successfully, but these errors were encountered: