Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytest Django并不专门支持Django’;s多数据库和数据库访问失败不允许_Python_Django_Pytest_Pytest Django - Fatal编程技术网

Python Pytest Django并不专门支持Django’;s多数据库和数据库访问失败不允许

Python Pytest Django并不专门支持Django’;s多数据库和数据库访问失败不允许,python,django,pytest,pytest-django,Python,Django,Pytest,Pytest Django,我试图使用非托管(只读)模型运行multi-db设置测试。运行测试时,serailizers.py中的导入功能失败,无法访问测试数据库 设置.py pytest.ini models.py 序列化程序.py 服务.py tests.py 错误 apps/tests/tests.py:4:in 从apps.views导入( :971:in_find_和_load??? :955:在“查找”和“加载”中解锁??? :665:in_load_unlocked??? /tmp/lib/python3.6

我试图使用非托管(只读)模型运行multi-db设置测试。运行测试时,serailizers.py中的导入功能失败,无法访问测试数据库

设置.py pytest.ini models.py 序列化程序.py 服务.py tests.py 错误
apps/tests/tests.py:4:in
从apps.views导入(
:971:in_find_和_load???
:955:在“查找”和“加载”中解锁???
:665:in_load_unlocked???
/tmp/lib/python3.6/site-packages/\u-pytest/assertion/rewrite.py:149:in-exec\u-module-exec(co,module.\uu-dict\uu)
应用/视图。py:6:in
从apps.serializers导入(
:971:in_find_和_load???
:955:在“查找”和“加载”中解锁???
:665:in_load_unlocked???
/tmp/lib/python3.6/site-packages/\u-pytest/assertion/rewrite.py:149:in-exec\u-module-exec(co,module.\uu-dict\uu)
应用程序/序列化程序。py:9:in
类序列化程序(序列化程序.序列化程序):
apps/serializer.py:13:在SearchSerializer中
required=False,style={'base_template':'select_multiple.html'}
/tmp/lib/python3.6/site-packages/rest\u-framework/fields.py:1476:in\u-init__
超级(多回音场,自我)。\uuuuu初始值(*args,**kwargs)
/tmp/lib/python3.6/site-packages/rest\u-framework/fields.py:1417:in\u-init__
自我选择
/tmp/lib/python3.6/site packages/rest\u framework/fields.py:1453:in\u set\u选项
self.grouped_choices=to_choices_dict(选项)
/tmp/lib/python3.6/site packages/rest\u framework/fields.py:150:in to\u choices\u dict
选择中的选择:
/tmp/lib/python3.6/site-packages/django/db/models/query.py:274:in-iter-self.\u-fetch\u-all()
/tmp/lib/python3.6/site packages/django/db/models/query.py:1242:in\u fetch\u all
self.\u result\u cache=list(self.\u iterable\u class(self))
/tmp/lib/python3.6/site packages/django/db/models/query.py:181:in\uu iter__
编译器=queryset.query.get\u编译器(queryset.db)
/tmp/lib/python3.6/site-packages/django/db/models/sql/query.py:289:in-get\u编译器
返回connection.ops.compiler(self.compiler)(self,connection,using)
/tmp/lib/python3.6/site-packages/django/db/backends/base/operations.py:330:in-compiler
self.\u cache=import\u模块(self.compiler\u模块)
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80:in\uu-get__
res=instance.\uuuu dict\uuuu[self.name]=self.func(实例)
/tmp/lib/python3.6/site packages/django/db/backends/oracle/operations.py:587:在编译器_模块中
如果self.connection.features.has_fetch_offset_支持:
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80:in\uu-get__
res=instance.\uuuu dict\uuuu[self.name]=self.func(实例)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/features.py:63:in支持获取偏移量
return self.connection.oracle_version>=(12,2)
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80:in\uu-get__
res=instance.\uuuu dict\uuuu[self.name]=self.func(实例)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/base.py:311:in-oracle\u版本
使用self.temporary_connection():
/usr/local/lib/python3.6/contextlib.py:81:in\uu输入__
返回下一个(self.gen)
/tmp/lib/python3.6/site packages/django/db/backends/base/base.py:593:处于临时连接中
使用self.cursor()作为光标:
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:256:in-cursor
返回自我。_cursor()
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:233:in\u游标
self.sure_连接()
E失败:不允许访问数据库,请使用“django_db”标记或“db”或“transactional_db”装置启用它。

首先,您同时使用两个测试库:unittest和pytest。
TestCase
RequestFactory
以及
setUp()
是unittest模块的一部分。db fixture的行为很奇怪,但是您是否尝试将
db
fixture作为参数传递给测试函数?例如:``def test\u my_function(db):``首先,您同时使用两个测试库:unittest和pytest。
TestCase
RequestFactory
以及
setUp()
是unittest模块的一部分。db fixture的行为很奇怪,但是您是否尝试将
db
fixture作为参数传递给测试函数?例如:``def test\u my_function(db):```
 if 'test' in sys.argv or 'test_coverage' in sys.argv: 
DATABASES = {                                                       
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db.sqlite3',
    },
    'test_db': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db.sqlite3',
    }
}
INSTALLED_APPS += ['test']
[pytest]
DJANGO_SETTINGS_MODULE=apps.settings
from django.db import models

class TestModelA(models.Model):
    testid = models.CharField(max_length=200)
    class Meta:
        managed = False
        db_table = 'TestD'
from rest_framework import serializers

from apps.services import get_testid

class SearchSerializer(serializers.Serializer):

application_names = serializers.MultipleChoiceField(
    choices=get_testid(),
    required=False, style={'base_template': 'select_multiple.html'}
)
from apps.models import TestModelA

def get_testid():
    return TestModelA.objects.values_list('testid ', flat=True)
import pytest

from django.test import RequestFactory, TestCase

from appsviews import (
    details
)

class MyTestCase(TestCase):

    def setUp(self):
        self.request = RequestFactory().get('/details/')

    @pytest.mark.django_db
    def test_my_function(self):
        response = details(self.request)
        self.assertEqual(response.status_code, 200)
apps/tests/tests.py:4: in <module>
from apps.views import (
<frozen importlib._bootstrap>:971: in _find_and_load???
<frozen importlib._bootstrap>:955: in _find_and_load_unlocked ???
<frozen importlib._bootstrap>:665: in _load_unlocked ???
/tmp/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:149: in exec_module exec(co, module.__dict__)
apps/views.py:6: in <module>
from apps.serializers import (
<frozen importlib._bootstrap>:971: in _find_and_load ???
<frozen importlib._bootstrap>:955: in _find_and_load_unlocked ???
<frozen importlib._bootstrap>:665: in _load_unlocked ???
/tmp/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:149: in exec_module exec(co, module.__dict__)
apps/serializers.py:9: in <module>
class Serializer(serializers.Serializer):
apps/serializers.py:13: in SearchSerializer
required=False, style={'base_template': 'select_multiple.html'}
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:1476: in __init__
super(MultipleChoiceField, self).__init__(*args, **kwargs)
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:1417: in __init__
self.choices = choices
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:1453: in _set_choices
self.grouped_choices = to_choices_dict(choices)
/tmp/lib/python3.6/site-packages/rest_framework/fields.py:150: in to_choices_dict
for choice in choices:
/tmp/lib/python3.6/site-packages/django/db/models/query.py:274: in __iter__ self._fetch_all()
/tmp/lib/python3.6/site-packages/django/db/models/query.py:1242: in _fetch_all
self._result_cache = list(self._iterable_class(self))
/tmp/lib/python3.6/site-packages/django/db/models/query.py:181: in __iter__
compiler = queryset.query.get_compiler(queryset.db)
/tmp/lib/python3.6/site-packages/django/db/models/sql/query.py:289: in get_compiler
return connection.ops.compiler(self.compiler)(self, connection, using)
/tmp/lib/python3.6/site-packages/django/db/backends/base/operations.py:330: in compiler
self._cache = import_module(self.compiler_module)
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80: in __get__
res = instance.__dict__[self.name] = self.func(instance)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/operations.py:587: in compiler_module
if self.connection.features.has_fetch_offset_support:
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80: in __get__
res = instance.__dict__[self.name] = self.func(instance)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/features.py:63: in has_fetch_offset_support
return self.connection.oracle_version >= (12, 2)
/tmp/lib/python3.6/site-packages/django/utils/functional.py:80: in __get__
res = instance.__dict__[self.name] = self.func(instance)
/tmp/lib/python3.6/site-packages/django/db/backends/oracle/base.py:311: in oracle_version
with self.temporary_connection():
/usr/local/lib/python3.6/contextlib.py:81: in __enter__
return next(self.gen)
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:593: in temporary_connection
with self.cursor() as cursor:
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:256: in cursor
return self._cursor()
/tmp/lib/python3.6/site-packages/django/db/backends/base/base.py:233: in _cursor
self.ensure_connection()
E   Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.