使用 unittest 测试烧瓶应用程序时出错 [英] Error while testing flask application with unittest
问题描述
我有
<块引用>ModuleNotFoundError: 没有名为project"的模块
尝试运行 test_req.py 时我的项目结构是:
├──实例/│ ├──烧瓶.cfg├──项目/│ ├── __init__.py│ ├── base_processing.py│ ├──模型.py|├── 视图.py│ ├──模板/│ │ ├── base.html│ │ ├── login.html│ │ ├── note.html│ │ ├── notes.html│ └── 静态/│ ││ └── 测试/│ ├── test_req.html├── 运行.py
我的 UnitTest 文件是:
# project/test_req.py导入单元测试导入操作系统从项目导入应用程序from project.models import db, User, Note从 project.views 导入 *TEST_DB = 'test.db'类 RequestTests(unittest.TestCase):@类方法def setUpClass(cls):app.config['TESTING'] = Trueapp.config['WTF_CSRF_ENABLED'] = Falseapp.config['DEBUG'] = Falseapp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \os.path.join(app.config['BASEDIR'], TEST_DB)app.secret_key = 'staytrue'cls.app = app.test_client()定义设置(自我):db.create_all()def 拆解(自我):db.drop_all()def test_main_page(self):response = self.app.get('/', follow_redirects=True)self.assertEqual(response.status_code, 200)def test_auth(self):u = 用户(用户名 =测试名1",密码 =1234567",电子邮件 =网络@mail.com")db.session.add(u)db.session.commit()response = self.app.post('/login', data=dict(username='testname1', password='1234567'), follow_redirects=True)使用 self.app.session_transaction() 作为 sess:self.assertEqual(sess['username'],'testname1')如果 __name__ == "__main__":单元测试.main()
当我从我的根目录运行它时,我的测试也可以很好地使用nose2.这也是我第一次以这种方式组织我的项目布局.
Module 不是文件夹,它应该是一个 .py
文件.由于您没有 project.py
文件,因此您不应指定 from project import app
.
指定from project import app
意味着有project.py
文件,你想从这个文件导入类app
.>
如果您的 test_req.py
和 app.py
文件位于同一文件夹中,则只需在您的 import app
中使用:代码>test_req.py
也替换:
from project.models import db, User, Note从 project.views 导入 *
到
from models import db, User, Note从视图导入 *
进一步阅读:
/folder1/MyPythonFile1.py
文件看起来像这样:class Class1:def __init__(self):经过类 Class2:def __init__(self):经过
和
/folder1/folder11/MyPythonFile2.py
文件看起来像这样:类 Class3:def __init__(self):经过
文件
/folder2/MyApp.py
使用上述文件中的类,如下所示:from folder1.MyPythonFile1 import Class1从 folder1.folder11.MyPythonFile2 导入 Class3obj1 = Class1()obj3 = Class3()
将此示例应用于您的特定案例并相应地更新您的导入.
I have
ModuleNotFoundError: No module named 'project'
while trying to run test_req.py My project structure is:
├── instance/ │ ├── flask.cfg ├── project/ │ ├── __init__.py │ ├── base_processing.py │ ├── models.py | ├── views.py │ ├── templates/ │ │ ├── base.html │ │ ├── login.html │ │ ├── note.html │ │ ├── notes.html │ └── static/ │ │ │ └── tests/ │ ├── test_req.html ├── run.py
My UnitTest file is:
# project/test_req.py import unittest import os from project import app from project.models import db, User, Note from project.views import * TEST_DB = 'test.db' class RequestTests(unittest.TestCase): @classmethod def setUpClass(cls): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \ os.path.join(app.config['BASEDIR'], TEST_DB) app.secret_key = 'staytrue' cls.app = app.test_client() def setUp(self): db.create_all() def tearDown(self): db.drop_all() def test_main_page(self): response = self.app.get('/', follow_redirects=True) self.assertEqual(response.status_code, 200) def test_auth(self): u = User(username='testname1', password='1234567', email='cyber@mail.com') db.session.add(u) db.session.commit() response = self.app.post('/login', data=dict(username='testname1', password='1234567'), follow_redirects=True) with self.app.session_transaction() as sess: self.assertEqual(sess['username'], 'testname1') if __name__ == "__main__": unittest.main()
Also my test work just fine with nose2, when I run it from my root directory. Also this is the first time I'm organizing my project layout this way.
解决方案Module is not a folder, it should be a
.py
file. As you don't haveproject.py
file, you should not specifyfrom project import app
.Specifying
from project import app
means that there isproject.py
file and you want to import classapp
from this file.if your
test_req.py
andapp.py
files are located in the same folder, then just use:import app
in yourtest_req.py
Also replace:
from project.models import db, User, Note from project.views import *
to
from models import db, User, Note from views import *
Further reading:
Also, I would recommend to use PyCharm Community Edition, it is free, multilpatform and open source, and will help you to solve such tasks just via two mouse clicks.
Assume we have the following project structure in the root folder of our project:
/folder1/MyPythonFile1.py /folder1/folder11/MyPythonFile2.py /folder2/MyApp.py
/folder1/MyPythonFile1.py
file looks like that:class Class1: def __init__(self): pass class Class2: def __init__(self): pass
And
/folder1/folder11/MyPythonFile2.py
file looks like that:class Class3: def __init__(self): pass
File
/folder2/MyApp.py
uses classes from aforecited files and looks like that:from folder1.MyPythonFile1 import Class1 from folder1.folder11.MyPythonFile2 import Class3 obj1 = Class1() obj3 = Class3()
Apply this example to your particular case and update your imports accordingly.
这篇关于使用 unittest 测试烧瓶应用程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!