ImportError:无法导入测试模块

时间:2020-01-02 23:46:47

标签: python django

这是我的项目结构:

enter image description here

self

我已经使用# Create your tests here. from django.contrib.auth.models import User from django.test import TestCase from g_attend.website.models import Profile class TestIndex(TestCase): def setUp(self): new_user = User.objects.create_user(username='user', password='pass') Profile.objects.create(user=new_user, type="Admin", full_name="Ahmed Wagdy") def test_visit_unauthorised(self): response = self.client.get('/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'home.html') 创建了两个应用python manage.py startappwebsite 现在,当我将测试添加到panel时,如下所示:

website.tests.py

但是我尝试运行测试时遇到此错误:# Create your tests here. from django.contrib.auth.models import User from django.test import TestCase from g_attend.website.models import Profile class TestIndex(TestCase): def setUp(self): new_user = User.objects.create_user(username='user', password='pass') Profile.objects.create(user=new_user, type="Admin", full_name="Ahmed Wagdy") def test_visit_unauthorised(self): response = self.client.get('/') self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'home.html')

1 个答案:

答案 0 :(得分:2)

g_attend 是一个模块,但是名为'website'的django应用位于外部,与所有目录一样位于 root 目录中应用程序存在。

在您的tests.py内部网站中,更改以下导入

from g_attend.website.models import Profile

from .models import Profile

from website.models import Profile

这将解决“无法导入模块” 的问题。