简体   繁体   中英

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?

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

I've been looking into this as well. The thing is that python unittest pytest and nose are not alternative to Django tests, because they would not be able to load everything Django tests do.

Django Test Runner might work for you: https://marketplace.visualstudio.com/items?itemName=Pachwenko.django-test-runner

-- I was having trouble with this still since my project root does not directly contain my app(s), but judging on your project structure may work for you.

Here is generic way to get Django tests to run with full vscode support

  1. Configure python tests
    1. Choose unittest
    2. Root Directory
    3. test*.py
  2. Then each test case will need to look like the following:
from django.test import TestCase

class views(TestCase):

    @classmethod
    def setUpClass(cls):
        import django
        django.setup()

    def test_something(self,):
        from user.model import something
        ...

Any functions you want to import have to be imported inside the test case (like shown). The setUpClass runs before the test class is setup and will setup your django project. Once it's setup you can import functions inside the test methods. If you try to import models/views at the top of your script, it will raise an exception since django isn't setup. If you have any other preinitialization that needs to run for your django project to work, run it inside setUpClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM