【问题标题】:Run Django tests in the VSCode Test explorer?在 VSCode 测试资源管理器中运行 Django 测试?
【发布时间】:2020-05-13 08:11:25
【问题描述】:

我正在尝试在 VSCode 测试资源管理器中运行 Django 单元测试,此外,我希望 CodeLens 的“运行测试”按钮出现在每个测试上方。 enter image description here 但是,在测试资源管理器中,当我按下播放按钮时,会显示错误: “没有进行任何测试”No Tests were Ran

我的目录结构是:

  • 工作区_文件夹
    • settings.json
    • 回购
      • python_module_1
        • 子模块
          • 测试
            • test_a.py

我正在使用 unittest 框架。 我的 Settings.json 看起来像这样:

{
    "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,
}

当我按下绿色的“播放”按钮时Test Explorer Play Button Python 测试日志输出显示消息“启动的线程中未处理的异常” Unhandled Exception in thread started by 我正在使用 pipenv 虚拟环境。 如何在 VSCode 测试资源管理器中运行这些 Django 测试?

我看到使用 pyTest 是 unittest 的替代品,如何轻松设置它作为替代品?

【问题讨论】:

    标签: django unit-testing visual-studio-code virtualenv pipenv


    【解决方案1】:

    我也一直在研究这个。问题是 python unittest pytest 和 nose 不能替代 Django 测试,因为它们无法加载 Django 测试所做的一切。

    Django Test Runner 可能对你有用: https://marketplace.visualstudio.com/items?itemName=Pachwenko.django-test-runner

    -- 因为我的项目根目录不直接包含我的应用程序,所以我仍然遇到了问题,但判断您的项目结构可能对您有用。

    【讨论】:

      【解决方案2】:

      请考虑以下检查:

      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
      

      现在它应该可以按您的意愿工作了。 你可以看到这个stackoverflow链接:

      https://stackoverflow.com/questions/55837922/vscode-pytest-test-discovery-fails
      

      【讨论】:

        【解决方案3】:

        这是让 Django 测试在 完整 vscode 支持下运行的通用方法

        1. 配置 python 测试
          1. 选择单元测试
          2. 根目录
          3. test*.py
        2. 那么每个测试用例需要如下所示:
        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
                ...
        

        您要导入的任何函数必须导入到测试用例中(如图所示)。 setUpClass 在设置测试类之前运行,并将设置您的 django 项目。设置完成后,您可以在测试方法中导入函数。如果您尝试在脚本顶部导入模型/视图,则会引发异常,因为 django 未设置。如果您有任何其他需要运行以使您的 django 项目工作的预初始化,请在 setUpClass

        中运行它

        【讨论】:

          猜你喜欢
          • 2014-06-15
          • 2018-05-03
          • 1970-01-01
          • 1970-01-01
          • 2021-10-10
          • 1970-01-01
          • 1970-01-01
          • 2020-09-13
          • 1970-01-01
          相关资源
          最近更新 更多