Django ν
μ€νΈ λ¬λ κΈ°λ° ν
μ€νΈλ₯Ό pytestλ‘ λ³ννλ λ° μ΄λ €μμ κ²ͺκ³ μμ΅λλ€.
λλ κ·Έ νΌν
μ κΈ°λν κ²μ΄λ€
import pytest
pytestmark = pytest.mark.django_db
λ΄ μ½λμ μν μ€ν μΆμ μ λ§μ§λ§ λͺ¨λ(lib/frontend/decorators.py)
μνν μ μμ§λ§ κ·Έλ μ§ μμ΅λλ€. μ€ν μΆμ μ μλ λ΄ λͺ¨λ 4κ° λͺ¨λμ λ£μ΄λ λμμ΄ λμ§ μμμ΅λλ€.
pytest-djangoμμ λ°μ΄ν°λ² μ΄μ€ μ‘μΈμ€λ₯Ό μ μμ μΌλ‘ νμ©ν μ μλ λ°©λ²μ΄ μμ΅λκΉ?
vagrant@vagrant-ubuntu-trusty-64:/vagrant$ pytest
== test session starts ==
platform linux2 -- Python 2.7.6, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
Django settings: patient.settings (from ini file)
rootdir: /vagrant, inifile: pytest.ini
plugins: django-3.0.0, pythonpath-0.7.1
collected 265 items / 2 errors
[...]
_ ERROR collecting lib/tests/report/testfunc.py _
lib/tests/report/testfunc.py:6: in <module>
from report.func import generate_report_filename
lib/report/func.py:29: in <module>
from frontend import chart
lib/frontend/chart.py:294: in <module>
@pytest.mark.django_db
lib/frontend/decorators.py:20: in decorator
if Site.objects.get_current().id in (allowed_site_ids):
/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py:60: in get_current
return self._get_site_by_id(site_id)
/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py:39: in _get_site_by_id
site = self.get(pk=site_id)
/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py:127: in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:328: in get
num = len(clone)
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:144: in __len__
self._fetch_all()
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:965: in _fetch_all
self._result_cache = list(self.iterator())
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:238: in iterator
results = compiler.execute_sql()
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py:838: in execute_sql
cursor = self.connection.cursor()
/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py:162: in cursor
cursor = self.make_debug_cursor(self._cursor())
/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py:135: in _cursor
self.ensure_connection()
E Failed: Database access not allowed, use the "django_db" mark to enable it.
κΈμ, λΉμ μ νμ autouse μ‘°λͺ κΈ°λ₯Ό λ§λ€ μ μμ΅λλ€.
@pytest.fixture(autouse=True)
def enable_db_access(db):
pass
λΉμ μ λνμμ
@enkore μ λλ΅μ κ° κΈΈμ λλ€. μ€λͺ μμ FAQλ μΆκ°νμ΅λλ€.
http://pytest-django.readthedocs.io/en/latest/faq.html#how -can-i-give-database-access-to-all-my-tests-without-the-django-db-marker
ν μ€νΈμμ κ°μ Έμ€λ λͺ¨λμ κ°μ Έμ¬ λ DBμ μ°κ²°λ©λλ€. ν μ€νΈ μμ§ μ€μ μ΄λ―Έ νμ©λλλ‘ μ μμ μΌλ‘ DB μ‘μΈμ€λ₯Ό νμ±ννλ €λ©΄ μ΄λ»κ² ν΄μΌ ν©λκΉ? κ°μ¬ ν΄μ!
@Telofy κ°μ Έμ€κΈ°λ₯Ό db μ‘μΈμ€λ‘ νμνλ λ¬Έμ λ₯Ό ν΄κ²°ν μ μ΄ μμ΅λκΉ? κ°μ λ¬Έμ κ°...
μ£μ‘νμ§λ§ Django(λ° pytest-django)κ° μλνλ λ°©μμ μ λ’°ν μ μλ λ°©μμΌλ‘ λΆκ°λ₯ν©λλ€. μ μΌν μ μ ν μ루μ μ κ°μ Έμ€κΈ° μκ° λμ λ°μ΄ν°λ² μ΄μ€ 쿼리λ₯Ό μννμ§ μλ κ²μ λλ€. pytest-djangoλ pytests ν½μ€μ² μμ€ν μ κΈ°λ°μΌλ‘ ꡬμΆλμμΌλ©° pytest-djangoκ° ν½μ€μ²κ° κ°μ Έμ€κΈ° μ μ μ€νλλλ‘ ν μ μλ λ°©λ²μ μμ΅λλ€.
μ΄ λ¬Έμ μ λν μμΈν μ€λͺ
μ μ¬κΈ° λ΄ μ견μ μ°Έμ‘°νμμμ€.
https://github.com/pytest-dev/pytest-django/issues/499#issuecomment -322056315
μ΄λ¬ν 쿼리λ₯Ό νΌνκΈ° μν΄ νμ¬μ μ 체 νλ«νΌμ λ€μ μμ±νλ κ²μ μ λ₯λ ₯μ΄λ μκ° μμ°μ΄ μλλλ€. λ΄ μ루μ μ conftest.pyμμ λ§€μ° λͺ»μκΈ΄ μμμ΄ ν¨μΉμ λλ€.
from pytest_django.plugin import _blocking_manager
from django.db.backends.base.base import BaseDatabaseWrapper
_blocking_manager.unblock()
_blocking_manager._blocking_wrapper = BaseDatabaseWrapper.ensure_connection
λλ λν μ΄ λ°μ½λ μ΄ν°( @pytest.mark.django_db
μ ν¨κ»)λ₯Ό μ¬μ©νμ¬ ν
μ€νΈκ° μλͺ»λ λ°μ΄ν°λ² μ΄μ€μμ μ€νλκ³ μμ§ μμμ§ νμΈν©λλ€. μμΈ‘νκΈ°κ° μ½κ° 볡μ‘νκΈ° λλ¬Έμ
λλ€.
def assert_test_db(func):
@wraps(func)
def wrapper(*args, **kwargs):
assert connection.settings_dict['NAME'] == 'test_kf_server', connection.settings_dict
return func(*args, **kwargs)
return wrapper
@Telofy μμ μ κ°μ¬λ립λλ€. μ λ§ μ μ©ν©λλ€. λν μ 체 μ½λλ² μ΄μ€λ₯Ό λ€μ μμ±νλ κ²μ νΌνκ³ μΆμ΅λλ€!
κ°μ Έμ¬ λ db 쿼리λ₯Ό λ§λλ κ²μ λμ pytestλ‘ μ΄μ΄μ§ λΏλ§ μλλΌ μΆμ νκΈ° μ΄λ €μ΄ λ€λ₯Έ λΆλΆμμ λ¬Έμ λ₯Ό μΌμΌν¬ κ²μ λλ€. λΉμ μ λμ μκ°μ 보λΌκ±°μΌ.
μ 체 μ½λλ² μ΄μ€λ₯Ό λ€μ μμ±ν νμκ° μμ΅λλ€. μ§μ° λ©μλ/μμ±μ μ¬μ©νμ¬ ν΄κ²°ν μ μμ΅λλ€. κ°μ μμ²ν λκΉμ§ μ°κ²°νμ§ μλ μ½λμ λλ€. λν νκ°νμ§ μκ³ μΏΌλ¦¬ μΈνΈλ§ μ λ¬ν΄λ μλν©λλ€. count λ±κ³Ό κ°μ κ²μ νΌνμμμ€.
κ°μ₯ μ μ©ν λκΈ
κΈμ, λΉμ μ νμ autouse μ‘°λͺ κΈ°λ₯Ό λ§λ€ μ μμ΅λλ€.
λΉμ μ λνμμ