python - 《Flask Web开发》 第七章 单元测试error

查看:159
本文介绍了python - 《Flask Web开发》 第七章 单元测试error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

《Flask Web开发》第七章,学到最后程序能够成功运行,但是进行单元测试却报错。

相关文件app__init__.py代码:

from flask import Flask, render_template
from flask_bootstrap import Bootstrap
from flask_mail import Mail
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from config import config

bootstrap = Bootstrap()
mail = Mail()
moment = Moment()
db = SQLAlchemy()

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    
    bootstrap.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    db.init_app(app)
    
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)             #注册蓝本
    
    return app

teststest_basics.py文件代码:

import unittest
from flask import current_app
from app import create_app, db

class BasicsTestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        
    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()
        
    def test_app_exists(self):
        self.assertFalse(current_app is None)
        
    def test_app_is_testing(self):
        self.assertTrue(current_app.config['TESTING'])

运行python manage.py runserver 没有问题,但是进行单元测试就失败了。网上查了一下,查不到太多的错误解析,求解???

解决方案

看报错,就可以看出来了,config.py 的 config没有键值testing,
请检查下config.py文件,可能没有更新到test 版本。

这篇关于python - 《Flask Web开发》 第七章 单元测试error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆