【问题标题】:Test discovery fails using DRF APITestCase but not django's TestCase使用 DRF APITestCase 但不是 django 的 TestCase 测试发现失败
【发布时间】:2020-02-24 17:23:53
【问题描述】:

使用 Django Rest Framework 的 APITestCase 类,Visual Studio Code 不会发现我的 unittest 测试。我已经将 vscode 配置为使用 unittest 并为其提供了我的 django 应用程序的路径。我已经在 jedi 和 ms 的 python 语言服务器之间切换了。

我可以手动运行测试,使用 python manage.py test

如果我切换到使用 Django 提供的 django.test.TestCase,vscode 会发现测试并创建装饰。我还尝试了 rest_framework 的另外两个测试用例:APISimpleTestCase、APITransactionTestCase,但都没有成功。

我的测试类很简单,基本上如下:

from django.test import TestCase

# * cannot get vscode to discover tests with this
from rest_framework.test import APITestCase

service_path = "/api/v0.1/service"


# class PathLookupTests(TestCase):
class PathLookupTests(APISimpleTestCase):
    def test_responding(self):
        uri = "valid_uri"

        resp = self.client.get(f"{service_path}/?uri={uri}")
        self.assertEqual(resp.status_code, 200)

Python Test Log我看到过一次回溯,但不能重复:

  File "/Users/bfalk/miniconda3/envs/web-server-eval/lib/python3.8/site-packages/django/test/testcases.py", line 1123, in setUpClass
    super().setUpClass()
  File "/Users/bfalk/miniconda3/envs/web-server-eval/lib/python3.8/site-packages/django/test/testcases.py", line 197, in setUpClass
    cls._add_databases_failures()
  File "/Users/bfalk/miniconda3/envs/web-server-eval/lib/python3.8/site-packages/django/test/testcases.py", line 218, in _add_databases_failures
    cls.databases = cls._validate_databases()
  File "/Users/bfalk/miniconda3/envs/web-server-eval/lib/python3.8/site-packages/django/test/testcases.py", line 204, in _validate_databases
    if alias not in connections:
TypeError: argument of type 'ConnectionHandler' is not iterable

【问题讨论】:

    标签: django visual-studio-code django-rest-framework


    【解决方案1】:

    经过一番挖掘,我现在看到 django 的测试运行器在 vscode 中不起作用。 https://github.com/microsoft/vscode-python/issues/73

    至少对我来说,解决方案是使用pytest-django

    这是上面的示例,但使用的是 pytest-django(在设置我的 pytest.ini 文件以指向我的 django 应用程序之后)

    service_path = "/api/v0.1/service"
    
    def test_responding(client):
        uri = "valid_uri"
    
        resp = client.get(f"{service_path}/?uri={uri}")
        assert resp.status_code == 200
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      相关资源
      最近更新 更多