我想用pytest“按原样”使用现有的db,但每次开始测试时都会执行对它的操作。
我创建了 conftest.py :
import pytest
from project import settings
@pytest.fixture(scope='session')
def django_db_setup():
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'PSQLDB',
'HOST': '127.0.0.1',
'PORT': '5432',
}
添加了pytest.ini:
[pytest]
DJANGO_SETTINGS_MODULE=project.settings
这是tests.py:
@pytest.mark.django_db(transaction=False)
class TestExperiment(object):
@pytest.fixture(autouse=True)
def setup_stuff(self, db):
pass
def test_something(self):
x=Mymodel.objects.last()
assert x.p==0
为什么运行py.test app / tests.py会在DB上触发此操作?
如果params为None: return self.cursor.execute(sql)E psycopg2.NotSupportedError:无法截断在a中引用的表 外键约束E DETAIL:表“demo_process” 引用“sl2”。 E提示:截断表“demo_process” 同时,或使用TRUNCATE ... CASCADE。
/home/tec1/.virtualenvs/xx/lib/python3.5/site-packages/django/db/backends/utils.py:83: NotSupportedError
django.core.management.base.CommandError: Database PSQLDB couldn't be flushed. Possible reasons:
E * The database isn't running or isn't configured correctly.
E * At least one of the expected database tables doesn't exist.
E * The SQL was invalid.
E Hint: Look at the output of 'django-admin sqlflush'. That's the SQL this command wasn't able to run.
/home/tec1/.virtualenvs/xxx/lib/python3.5/site-packages/django/core/management/commands/flush.py:74: CommandError
============================================================= 1 passed, 1 error in 0.20 seconds ===================
数据库存在,如果我运行runserver它会连接没有问题。