Python Django call_命令测试文件,ModuleNotFoundError
我有一段芹菜代码,在后台运行Django测试,它看起来如下:Python Django call_命令测试文件,ModuleNotFoundError,python,django,celery,django-celery,Python,Django,Celery,Django Celery,我有一段芹菜代码,在后台运行Django测试,它看起来如下: @shared_task() def execute_test_from(files): if isinstance(files, list): dir1 = ['app1.tests.' + file for file in files if file + '.py' in os.listdir(path1)] dir2 = ['app2.tests.' + file for file in
@shared_task()
def execute_test_from(files):
if isinstance(files, list):
dir1 = ['app1.tests.' + file for file in files if file + '.py' in os.listdir(path1)]
dir2 = ['app2.tests.' + file for file in files if file + '.py' in os.listdir(path2)]
fs = dir1 + dir2
call_command('test', ' '.join(fs), verbosity=3, keepdb=True)
|-- src
|-- manage.py
|-- myproject
|-- __init__.py
|-- settings.py
|-- urls.py
|-- app1
|-- __init__.py
|-- tests
|-- __init__.py
|-- test_1.py
|-- test_2.py
|-- app2
|-- __init__.py
|-- tests
|-- __init__.py
|-- test_1.py
|-- test_2.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2',
]
if 'test' in sys.argv or 'test_coverage' in sys.argv:
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
path1
和path2
包含测试,files
是包含测试的特定文件的列表
path1 = os.path.join(os.getcwd(), "app1/tests")
path2 = os.path.join(os.getcwd(), 'app2/tests')
# files will look like this
files = ['app1.tests.test_1', 'app1.tests.test_2', 'app2.tests.test_1', 'app2.tests.test_2']
我的文件夹结构如下所示:
@shared_task()
def execute_test_from(files):
if isinstance(files, list):
dir1 = ['app1.tests.' + file for file in files if file + '.py' in os.listdir(path1)]
dir2 = ['app2.tests.' + file for file in files if file + '.py' in os.listdir(path2)]
fs = dir1 + dir2
call_command('test', ' '.join(fs), verbosity=3, keepdb=True)
|-- src
|-- manage.py
|-- myproject
|-- __init__.py
|-- settings.py
|-- urls.py
|-- app1
|-- __init__.py
|-- tests
|-- __init__.py
|-- test_1.py
|-- test_2.py
|-- app2
|-- __init__.py
|-- tests
|-- __init__.py
|-- test_1.py
|-- test_2.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2',
]
if 'test' in sys.argv or 'test_coverage' in sys.argv:
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
当然,当我运行/manage.py test app1.tests.test_1 app2.tests.tests_2
时,我只运行了两个不同的文件和测试。然而,当我像上面芹菜代码中那样使用call\u命令尝试相同的过程时,我遇到了错误:test\u 1 app1(unittest.loader.\u FailedTest)
ImportError:无法导入测试模块:test\u 1 app1
我的回溯最新调用读取:ModuleNotFoundError:没有名为“app1.tests.test\u 1 app1”的模块。
。只有当文件
只有一个文件要测试时,代码才起作用,多个文件会产生这些错误。
我尝试从app1
中删除\uuuuu init\uuuuuuuuuupy
,但错误保持不变,如果我从测试中删除它,Django将不再将测试识别为包含python代码
与我的测试关联的唯一特定设置如下所示:
@shared_task()
def execute_test_from(files):
if isinstance(files, list):
dir1 = ['app1.tests.' + file for file in files if file + '.py' in os.listdir(path1)]
dir2 = ['app2.tests.' + file for file in files if file + '.py' in os.listdir(path2)]
fs = dir1 + dir2
call_command('test', ' '.join(fs), verbosity=3, keepdb=True)
|-- src
|-- manage.py
|-- myproject
|-- __init__.py
|-- settings.py
|-- urls.py
|-- app1
|-- __init__.py
|-- tests
|-- __init__.py
|-- test_1.py
|-- test_2.py
|-- app2
|-- __init__.py
|-- tests
|-- __init__.py
|-- test_1.py
|-- test_2.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2',
]
if 'test' in sys.argv or 'test_coverage' in sys.argv:
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
我假设call_command
的行为与从命令行运行多个测试的方式相同,我还可以尝试什么?多谢各位