长话短说,我正在尝试在docker容器中运行测试,例如:
./manage.py test
但出现错误:
/app/gauss # ./manage.py test
nosetests --where=tests/ --verbosity=2 --with-coverage --cover-package=gauss.catalogues --cover-package=gauss.requests --cover-package=gauss.core --cover-xml --cover-xml-file=gauss_coverage.xml --with-xunit --xunit-file=gauss_xunit.xml
Creating test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py:259: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
RuntimeWarning
Got an error creating the test database: database "test_gauss" already exists
Type 'yes' if you would like to try deleting the test database 'test_gauss', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
Traceback (most recent call last):
File "./manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
...
File "/app/gauss/gauss/catalogues/migrations/0001_initial.py", line 13, in <module>
class Migration(migrations.Migration):
File "/app/gauss/gauss/catalogues/migrations/0001_initial.py", line 57, in Migration
('web_url', models.CharField(blank=True, max_length=100, validators=[gauss.catalogues.validators.validate_url], verbose_name='web_url')),
AttributeError: module 'gauss' has no attribute 'catalogues'
'gauss'是我的django项目名称,而'catalogoues'是其中的应用程序。当我在本地计算机上运行相同命令时,错误不会出现。
所有项目结构:
项目设置:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.postgres',
'django_nose',
'mptt',
'sorl.thumbnail',
'rest_framework',
'webpack_loader',
'gauss.catalogues',
'gauss.ui.general',
'gauss.requests'
]
...
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = [
'--where=tests/',
'--verbosity=2',
'--with-coverage',
'--cover-package=gauss.catalogues',
'--cover-package=gauss.requests',
'--cover-package=gauss.core',
'--cover-xml',
'--cover-xml-file=gauss_coverage.xml',
'--with-xunit',
'--xunit-file=gauss_xunit.xml',
]
部分迁移文件导致错误:
# Generated by Django 2.0.2 on 2018-09-04 12:17
from django.conf import settings
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
import django.db.models.manager
import gauss.catalogues.model_fields
import gauss.catalogues.validators
import mptt.fields
class Migration(migrations.Migration):
..
migrations.CreateModel(
name='GSClient',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('deleted', models.BooleanField(default=False)),
('added_date', models.DateTimeField(blank=True, null=True)),
('name', models.CharField(max_length=100, verbose_name='Название')),
('web_url', models.CharField(blank=True, max_length=100, validators=[gauss.catalogues.validators.validate_url],)),
('email_domain', models.CharField(max_length=100, validators=[gauss.catalogues.validators.domain_validator], verbose_name='Почтовый домен')),
],
options={
'db_table': 'gs_catalogues_clients',
},
),
导致错误的模型
from django.db import models
from django.core.exceptions import ValidationError
from django.contrib.contenttypes.models import ContentType
from gauss.catalogues import validators, model_fields
from gauss.core import mixins
class GSClient(mixins.DeletedMixin, mixins.GSAddedDateMixin, models.Model):
name = models.CharField(
max_length=100,
)
web_url = models.CharField(
max_length=100,
blank=True,
validators=[validators.validate_url],
)
email_domain = models.CharField(
max_length=100,
validators=[validators.domain_validator],
blank=False,
null=False,
)
structure = models.ForeignKey(
'catalogues.GSClientStructure',
null=True,
related_name="structure_client",
on_delete=models.SET_NULL,
)
class Meta:
db_table = "gs_catalogues_clients"
我从外壳检查了模块“ gauss”的所有属性,并且具有模块目录:
>>> import gauss
>>> print(gauss)
<module 'gauss' from '/app/gauss/gauss/__init__.py'>
>>> print(gauss.catalogues)
<module 'gauss.catalogues' from '/app/gauss/gauss/catalogues/__init__.py'>
>>> print(dir(gauss))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'catalogues', 'core', 'requests', 'settings', 'settings_local', 'ui']
答案 0 :(得分:0)
好的。我找到了解决方案。问题出在我的配置中
'--where=tests/'
需要
'--where=gauss/tests/'