私の問題は以下の1つです。テストを実行しようとすると、データベースのアクセス許可がなく、そのフィクスチャを追加する必要があると表示されます。問題は、私が持っている可能な方法にそのフィクスチャを追加したことですが、それでも何もありません。したがって、このマーカーをどこに追加すればよいかわからないと思います。
私がこれらのオブジェクトの作成と呼んでいるクラスのメソッドにあるべきではありません:test_can_get_page_details
?
これらの属性へのユーザーの割り当てからUserFactoryを削除すると、テストは機能します。
[pytest]
Django_SETTINGS_MODULE=core.settings_test
import pytest
from bs4 import BeautifulSoup
import datetime
from Django.core.urlresolvers import reverse
from interface.tests import factories
class TestPageDetail:
@pytest.fixture
def defaults(self, db):
page = factories.PageFactory()
url = page.get_absolute_url()
return {
'page': page,
'url': url
}
@pytest.mark.integration
def test_can_get_page_details(self, defaults, db, admin_client):
response = admin_client.get(defaults['url'])
assert 200 == response.status_code
import pytest
@pytest.fixture
def initial_data(db):
from interface.management.commands.initial_data import Command as InitialData
InitialData().handle()
@pytest.fixture
def page_and_variable(db, initial_data):
from interface.tests.factories import PageFactory, VariableFactory
page = PageFactory()
variable = VariableFactory()
return dict(
page=page,
variable=variable
)
import factory
import datetime
import pytest
from interface import models
class UserFactory(factory.Django.DjangoModelFactory):
class Meta:
model = models.User
Django_get_or_create = ('username',)
username = factory.Sequence(lambda n: "staffman-%d" % n)
email = factory.LazyAttribute(lambda obj: '%[email protected]' % obj.username)
first_name = factory.Sequence(lambda n: "John-%d" % n)
last_name = "Doe"
password = "1234"
@classmethod
def _create(cls, model_class, *args, **kwargs):
"""Override the default ``_create`` with our custom call."""
manager = cls._get_manager(model_class)
# default is ``manager.create(*args, **kwargs)``
user = manager.create_user(*args, **kwargs)
user.is_staff = True
user.is_superuser = True
user.save()
return user
class PageFactory(factory.Django.DjangoModelFactory):
class Meta:
model = models.Page
slug = factory.Sequence(lambda n: "slug-%d" % n)
title = factory.Sequence(lambda n: "title-%d" % n)
content = factory.Sequence(lambda n: "Page content with a number: %d" % n)
created_by = UserFactory()
updated_by = UserFactory()
class VariableFactory(factory.Django.DjangoModelFactory):
class Meta:
model = models.Variable
name = factory.Sequence(lambda n: "some_variable_%d" % n)
content = factory.Sequence(lambda n: "Variable content with a number: %d" % n)
/documents/envs/atomenv/bin/python /documents/applications/pycharm-2017.1.2/helpers/pycharm/_jb_pytest_runner.py --path test_pages.py -- --nomigrations
Testing started at 1:14 PM ...
Launching py.test with arguments --nomigrations test_pages.py in /documents/projects/atom/interface/tests
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
Django settings: core.settings_test (from environment variable)
rootdir: /documents/projects/atom, inifile: pytest.ini
plugins: mock-1.6.0, Django-3.1.2, cov-2.5.1
interface/tests/test_pages.py:None (interface/tests/test_pages.py)
test_pages.py:5: in <module>
from interface.tests import factories
factories.py:21: in <module>
class PageFactory(factory.Django.DjangoModelFactory):
factories.py:28: in PageFactory
created_by = UserFactory()
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:69: in __call__
return cls.create(**kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:623: in create
return cls._generate(True, attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:548: in _generate
obj = cls._prepare(create, **attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:523: in _prepare
return cls._create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/Django.py:179: in _create
return cls._get_or_create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/Django.py:170: in _get_or_create
obj, _created = manager.get_or_create(*args, **key_fields)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/manager.py:85: in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:464: in get_or_create
return self.get(**lookup), False
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:374: in get
num = len(clone)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:232: in __len__
self._fetch_all()
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:1105: in _fetch_all
self._result_cache = list(self._iterable_class(self))
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:53: in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/sql/compiler.py:874: in execute_sql
cursor = self.connection.cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/backends/base/base.py:254: in cursor
return self._cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/backends/base/base.py:229: 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.
collected 0 items / 1 errors
==================================== ERRORS ====================================
________________ ERROR collecting interface/tests/test_pages.py ________________
test_pages.py:5: in <module>
from interface.tests import factories
factories.py:21: in <module>
class PageFactory(factory.Django.DjangoModelFactory):
factories.py:28: in PageFactory
created_by = UserFactory()
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:69: in __call__
return cls.create(**kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:623: in create
return cls._generate(True, attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:548: in _generate
obj = cls._prepare(create, **attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:523: in _prepare
return cls._create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/Django.py:179: in _create
return cls._get_or_create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/Django.py:170: in _get_or_create
obj, _created = manager.get_or_create(*args, **key_fields)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/manager.py:85: in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:464: in get_or_create
return self.get(**lookup), False
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:374: in get
num = len(clone)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:232: in __len__
self._fetch_all()
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:1105: in _fetch_all
self._result_cache = list(self._iterable_class(self))
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/query.py:53: in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/models/sql/compiler.py:874: in execute_sql
cursor = self.connection.cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/backends/base/base.py:254: in cursor
return self._cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/Django/db/backends/base/base.py:229: 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.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.33 seconds ============================
Process finished with exit code 0
私は問題を見つけました。ファクトリを手動で作成する代わりに:
created_by = UserFactory()
SubFactoryを使用する必要があります
created_by = factory.SubFactory(UserFactory)