【问题标题】:VSCode pytest test discovery fails when debugging Django tests调试 Django 测试时,VSCode pytest 测试发现失败
【发布时间】:2021-01-21 22:06:23
【问题描述】:

我正在尝试调试我的第一个 django 测试,但 VSCode 正在返回:在 0.000 秒内运行 0 个测试。 另一方面,当我在 VSCode 终端或外部使用集成的 git bash(我使用的是 Windows 操作系统)时,它会返回: Ran 1 test in 0.0014s。它还回显 FAILED 和测试中有效错误的回溯。

我的 vscode launch.json:

{   
    ...
    "configurations": [
        ...
        {
            "name": "Django Tests",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}\\testprojectname\\manage.py",
            "args": [
                "test"
            ],
            "django": true,
        }
    ]
}

我的 vscode settings.json:

{
    "python.pythonPath": "C:\\Users\\user\\.virtualenvs\\backend-Vg-KBKTA\\Scripts\\python.exe",
    "python.testing.pytestEnabled": true,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": false,
    "python.testing.pytestArgs": [
        "testprojectname"
    ]
}
  • 请注意,C:\Users\user\.virtualenvs\backend-Vg-KBKTA\Scripts\python.exe 是 pipenv shell 的有效路径 [(1) 当我输入 bash 时会回显它: pipenv - -venv (2) 它在我启动 django 调试器时起作用]

到目前为止,我已经尝试过:

  1. 根据更新我的 pytest VSCode pytest test discovery fails
  2. 将我的 tests.py 文件的名称更改为 test_example.py 根据 Pytest - no tests ran
  3. 根据 (2) 中的相同链接将测试的类更改为前缀为 Test_

这是当前测试的样子


class Test_Contacts(unittest.TestCase):

    def test_create_contact_message(self):
        """Create a new contact message."""
        url = reverse('message-list')
        data = {
            'first_name': 'DabApps',
            'last_name': 'jack',
            'email': 'giehogho24h@gmo8y9tgail.com',
            'message': 'hi, how are you?',
            'message_type': 1
        }
        # this is just random code to cause an error
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Message.objects.count(), 1)
        self.assertEqual(Message.objects.get().name, 'DabApps')

【问题讨论】:

    标签: django visual-studio testing pytest pipenv


    【解决方案1】:

    我无法解决最初的问题,但我找到了一个很好的解决方法。我现在可以在测试代码上方看到“调试测试”。我不知道为什么,但可能是因为我安装了 pytest-django 或扩展 python test explorer ui。

    python 测试浏览器用户界面故事

    1. 我在 Google 上搜索了调试 django 测试的方法,最终找到了一条 stackoverflow 评论,让我看到了这个 https://github.com/Microsoft/vscode-python/issues/3911
    2. 那个 github 问题提到了一个适配器,这让我下载了 little fox 团队的 VS 代码扩展:“Visual Studio Code 的 Python 测试资源管理器”,截至 2021 年 1 月 24 日已下载 262,853 次。
    3. 现在,当我在 git bash 中运行 pytest --collect-only 时,我遇到了错误:尚未加载应用程序。这导致我第一次发现 pytest-django "Apps aren't loaded yet" when trying to run pytest-django

    pytest-django 故事

    1. 阅读 pytest-django 文档
    2. 制作了pytest.ini文件,内容如下:
    [pytest]
    DJANGO_SETTINGS_MODULE = testprojectname.settings
    python_files = tests.py test_*.py *_tests.py
    
    • testprojectname 是 django 项目的名称
    1. 现在我注意到我在 tests.py 中进行的测试在其上方有可点击的用于调试的单词。单击调试测试需要一段时间才能启动,但它允许代码在红点 VS 代码断点处停止。 clickable words for debugging above test

    2. 虽然这就是我所需要的,但我也注意到我现在可以从 VS 代码测试资源管理器运行测试,因此我不再需要在 bash 中使用 pipenv shell 来运行所有测试。 VS code test explorer found test

    【讨论】:

      【解决方案2】:

      这是让 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

      中运行它

      【讨论】:

        猜你喜欢
        • 2019-09-14
        • 2020-12-02
        • 2020-04-08
        • 1970-01-01
        • 2023-03-06
        • 2020-03-12
        • 2016-11-02
        • 2016-06-02
        • 2021-02-06
        相关资源
        最近更新 更多