Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Django tests in the VSCode Test explorer?

I am trying to run Django unit tests in the VSCode Test Explorer, also, I want the CodeLens 'Run Tests' button to appear above each test. enter image description here However, in the Test Explorer, When I press the Play button, an error displays: "No Tests were Ran" No Tests were Ran

My directory structure is:

  • Workspace_Folder
    • settings.json
    • repo
      • python_module_1
        • sub_module
          • tests
            • test_a.py

I am using the unittest framework. My Settings.json looks like this:

{
    "python.pythonPath": "/Users/nbonilla/.local/share/virtualenvs/koku-iTLe243o/bin/python",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "${workspaceFolder}/python_module_1/sub_module/"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true,
}

When I press the green "Play" button Test Explorer Play Button The Python Test Log Output shows the message "Unhandled exception in thread started by" Unhandled Exception in thread started by I am using a pipenv virtual environment. How do I run these Django Tests in the VSCode Test Explorer?

I saw that using pyTest is an alternative to unittest, how can this be set up easily as a replacement?

like image 207
Niiiick Avatar asked Jan 27 '20 22:01

Niiiick


People also ask

Can we run Django in Vscode?

You can create a customized launch profile in VS Code, which is also used for the inevitable exercise of debugging. Select the link and VS Code will prompt for a debug configuration. Select Django from the dropdown and VS Code will populate a new launch. json file with a Django run configuration.

How do I run a test case in Django?

Open /catalog/tests/test_models.py.TestCase , as shown: from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.

How do I run a Python test in Visual Studio?

Open a Python project. Once the project is loaded in Visual Studio, right-click your project in Solution Explorer and select the unittest or pytest framework from the Properties Test tab. If you use the pytest framework, you can specify test location and filename patterns using the standard pytest .


1 Answers

Please consider the following checks:

1- you should have __init__.py in your test directory

2- in vscode on test configuration use pytest framework

3- use:  pip install pytest-django     

4- copy pytest.ini in the root with this content:

# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = <your-web-project-name>.settings (like mysite.settings)
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py

Now it should work as you wish. you can see this stackoverflow link :

https://stackoverflow.com/questions/55837922/vscode-pytest-test-discovery-fails
like image 140
Pouyan Sepahvand Avatar answered Sep 22 '22 02:09

Pouyan Sepahvand