问题描述
我有一段在后台运行 Django 测试的 celery 代码,它看起来像这样:
@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)
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']
我的文件夹结构如下:
|-- 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
自然地,当我运行 ./manage.py test app1.tests.test_1 app2.tests.test_2
时,它可以工作,我只是运行了两个不同的带有测试的文件。但是,当我像上面的 celery 代码一样使用 call_command
尝试相同的过程时,我遇到了 ERROR: test_1 app1 (unittest.loader._FailedTest)
ImportError: Failed to import test module: test_1 app1
我的回溯最近调用读取:ModuleNotFoundError: No module named 'app1.tests.test_1 app1'
。该代码仅在 files
只有一个要测试的文件时有效,多个文件会产生这些错误。
我尝试从 __init__.py
中删除 app1
,但错误保持不变,如果我从 tests
中删除它,Django 不再将测试识别为包含 python 代码
与我的测试相关的唯一特定设置如下所示:
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
的行为方式与从命令行运行多个测试的方式相同,我还能尝试什么?谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)