Helps navigating pytest.mark.parametrize
entries and fixtures by virtual text and jump to declaration commands, using pytest
s cache and treesitter
.
pytest
is amazing! The only thing that bothers me from time to time is if there are many entries in the parametrization of the test.
If a test fails you might see for example:
test.py::test[None2-a1-b-c1-8]
Now you want to see what test case this actually corresponds to.
What I sometimes do is to go to the entries in pytest.mark.parametrize
and count the entries until I'm at the right one.
But this is really not nice and easy to make a mistake, we should let the computer do this for us.
Enter pytrize
.
Another issue is knowing where a certain fixture is defined and what it does.
Three things:
- Populates virtual text at the entries of
pytest.mark.parametrize
(see gif above), such that you can easily see which one is which. Done by callingPytrize
(andPytrizeClear
to clear them). Alternativelylua require('pytrize.api').set()
(andlua require('pytrize.api').clear()
). - Provides a command to jump to the corresponding entry in
pytest.mark.parametrize
based on the test-case id under the cursor (see gif above). Done by callingPytrizeJump
. Alternativelylua require('pytrize.api').jump()
. See the Input-section below for cases where the file-path is not available. - Provides a command to jump to the declaration of the fixture under the cursor (by name), see fixture below.
Done by calling
PytrizeJumpFixture
. Alternativelylua require('pytrize.api').jump_fixture()
.
For example using packer
:
use { -- pytrize {{{
'AckslD/nvim-pytrize.lua',
-- uncomment if you want to lazy load
-- cmd = {'Pytrize', 'PytrizeClear', 'PytrizeJump'},
-- uncomment if you want to lazy load but not use the commands
-- module = 'pytrize',
config = 'require("pytrize").setup()',
} -- }}}
Requires plenary.nvim
.
require("pytrize").setup
takes an optional table of settings which currently have the default values:
{
no_commands = false,
highlight = 'LineNr',
preferred_input = 'telescope',
}
where:
no_commands
can be set totrue
and the commandsPytrize
etc won't be declared.highlight
defines the highlighting used for the virtual text.preferred_input
which method to query input to prefer (if it's installed), see the Input-section below.
pytest
s cache is used to find the test-case ids (egtest.py::test[None2-a1-b-c1-8]
) which means that the tests have to be run at least once. Also old ids might confusepytrize
, but you can clear the cache withpytest --cache-clear
.treesitter
is used to find the correct entry inpytest.mark.parametrize
.
To jump to the declaration of a fixture under the cursor, do PytrizeJumpFixture
:
In some cases the file-path is not printed by pytest, for example when a test fails when it might look something like:
_________________________________ test[None2-a1-b-c1-9] _________________________________
or similar.
If you trigger to jump to the declaration of the parameters in this case pytrize
will find all files in the cache that matches this test-case id and if there is more than one ask you which one to jump to.
Currently three input methods are supported: