简体   繁体   English

导入错误:无法导入测试模块

[英]ImportError: Failed to import test modul

here is my project structure :这是我的项目结构:

在此处输入图片说明

# 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')

I've used python manage.py startapp to create both apps website and panel Now when I added tests to website.tests.py as follows :我已经使用python manage.py startapp创建了应用程序websitepanel现在当我向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')

but I try to run tests I get this error: ImportError: Failed to import test module: website.tests但我尝试运行测试时出现此错误: ImportError: Failed to import test module: website.tests

g_attend over here is a module, but the django app named 'website' is outside, that is in the root directory like all apps exist. g_attend在这里是一个模块,但名为“网站”的 django 应用程序在外面,它在目录中,就像所有应用程序都存在一样。

In your tests.py inside website, change the following import在您的网站内的tests.py ,更改以下导入

from g_attend.website.models import Profile to from g_attend.website.models import Profile

from .models import Profile or maybe from .models import Profile或者也许

from website.models import Profile

This will fix the 'cannot import module' problem.这将解决“无法导入模块”的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM