[Python] VSCode not discovering pytests for Django Project

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    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. [​IMG]

    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:


    Continue reading...

Compartilhe esta Página