Skip to content

Improve Django Unit Test Execution Pathing #103

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danielebra
Copy link

@danielebra danielebra commented Aug 15, 2025

When running neovim somewhere other than where manage.py is located can cause pathing issues when running tests. I think this can be improved by identifying where manage.py is located, relative to the test file location.

Consider the existing solution:

# Make sure we can import relative to current path
sys.path.insert(0, os.getcwd())

It will place the cwd of neovim into the system path, before executing the test.

Given a repository such as:

app/
├── manage.py
├── app/ 
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── foo/
│   ├── __init__.py
│   ├── models.py
│   ├── views.py
│   └── tests/
│       └── test_foo.py
└── bar/
    ├── __init__.py
    ├── models.py
    ├── views.py
    └── tests/
        └── test_bar.py

and with neovim launched from top of the repository and a test being ran in bar/tests/test_bar.py

it would result in a case_id:

/path/to/repo/app/bar/tests/test_bar.py::Something::test_something

that then results in an argv generation of

['neotest-python', 'app.bar.tests.test_bar.Something.test_something']

Note the prefix of app, which is the django project housing project in the given repository structure example

This would be different to what would of occured if the tests were ran with manage.py, because the path would start with bar instead of app.

In the current solution, one way around this is to cd into the app folder, before triggering neotest to run the test.

This pull request proposes adding a resolver to locate manage.py and perform relative file calculations from the location of manage.py, rather than the location of neovim's cwd

@afrischk this extends your implementation, what do you think? #51

@afrischk
Copy link
Contributor

@danielebra Nice, thank you. This seems to be a reasonable feature.

@danielebra
Copy link
Author

Thanks @afrischk

@rcarriga anything else needed in order to merge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants