VSCode not discovering pytests for Django Project

I am new to Pytest & Django, so all feedback and improvements are welcome.

I am trying to use VSCode's testing sidebar with my Django Project, but it is unable to find the pytest files. Here are the commands I've run to create this example project:

django-admin startproject LearningPytest

pip install django-pytest

Then I've created two files: pytest.ini and basic_tests.py, so my folder structure is as follows.

DjangoPyTesting/
└───LearningPytest/
    ├───LearningPytest/
    │   ├───__init__.py
    │   ├───asgi.py
    │   ├───basic_tests.py
    │   ├───settings.py
    │   ├───urls.py
    │   ├───wsgi.py
    ├───manage.py
    └───pytest.ini

pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE = LearningPytest.settings
python_files = tests.py test_*.py *_tests.py
python_classes = Test*
python_functions = test_*

basic_tests.py

class TestBasicMath:
    
    def setup_method(self, method):
        self.x = 2
        self.y = 3
    
    def test_addition(self):
        assert self.x + self.y == 5 

    def test_subtraction(self):
        assert self.y - self.x == 1

    def test_multiplication(self):
        assert self.x * self.y == 6

Here is the problem, I want to use VSCode's sidebar to run my tests, but it cannot find them. VSCode showing no tests found

This confuses me, because within my project if I run the following command: DjangoPytesting\LearningPytest>pytest it works successfully

platform win32 -- Python 3.11.5, pytest-7.4.4, pluggy-1.3.0
rootdir: C:\Users\{me}\Desktop\DjangoPytesting\LearningPytest
configfile: pytest.ini
plugins: anyio-4.3.0, django-4.9.0
collected 3 items

LearningPytest\basic_tests.py ...platform win32 -- Python 3.11.5, pytest-7.4.4, pluggy-1.3.0
rootdir: C:\Users\{me}\Desktop\DjangoPytesting\LearningPytest
configfile: pytest.ini
plugins: anyio-4.3.0, django-4.9.0
collected 3 items

LearningPytest\basic_tests.py ... 3 passed in 0.02s

But if I attempt to run the pytest one level higher, it cannot find any tests, and I think this may be related to the problem. DjangoPytesting>pytest

platform win32 -- Python 3.11.5, pytest-7.4.4, pluggy-1.3.0
rootdir: C:\Users\{me}\Desktop\DjangoPytesting
plugins: anyio-4.3.0, django-4.9.0
collected 0 items 
no tests ran in 0.01s

How can I get VSCode's sidebar to find my pytests, so I can run the testing there?

Unsuccessful attempted solutions:

The Python extension also offers support for discovering and running Django unit tests starting from VSCode 1.93. You can get your Django tests discovered with only a few additional setup steps:

  1. Set "python.testing.unittestEnabled": true, in your settings.json file.
  2. Add MANAGE_PY_PATH as an environment variable: Create a .env file at the root of your project.
  3. Add MANAGE_PY_PATH='<path-to-manage.py>' to the .env file, replacing <path-to-manage.py> with the path to your application's manage.py file.
  4. Add Django test arguments to "python.testing.unittestArgs": [] in the settings.json file as needed, and remove any arguments that are not compatible with Django.

PS:If your .venv file is not located at your project root please add "python.envFile": "${workspaceFolder}/<path-to-.env>" to your settings.json to ensure all your environmental variables get discovered.

Back to Top