烧瓶unittest错误404响应

时间:2020-07-27 15:44:04

标签: python flask python-unittest

我正试图开始为Flask应用程序编写单元测试。我正在使用unittest库。当我测试对/(索引)的请求时,它会获得正确的200响应代码,但是当我测试确实存在的任何其他URL时,我将得到错误的404页面和错误响应。

这是我的测试文件:

import os

import unittest
import flask
import flask_mcc
from . import mcc
from flask_mcc.models import db, Client
from flask_login import LoginManager, login_user, logout_user, current_user 
app = flask_mcc.create_app()
class BasicTests(unittest.TestCase):
 
    ############################
    #### setup and teardown ####
    ############################
 
    # executed prior to each test
    def setUp(self):
        app.config['TESTING'] = True
        app.config['WTF_CSRF_ENABLED'] = False
        app.config['DEBUG'] = False
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test_db.sqlite'
        app.register_blueprint(mcc.bp)
        self.app = app.test_client()
        #db.drop_all()
        db.create_all()

    # executed after each test
    def tearDown(self):
        db.drop_all()
 
    def login(self):
        test_user = User(username='test_user', google_sub='1', active=1, email='test@gmail.com')
        db.session.add(test_user)
        db.session.commit()
        return self.app.get(
            '/login/callback',
            follow_redirects=True
        )

        print(current_user.username)
###############
#### tests ####
###############


    def test_access_denied(self):
        response = self.app.get('/', follow_redirects=True)
        self.assertEqual(response.status_code, 401)
    

    def test_access_granted(self):
        with self.app:
            self.login()
            response = self.app.get('/', follow_redirects=True)
            self.assertEqual(response.status_code, 200)
 
    def test_no_clients(self):
        with self.app:
            self.login()
            response = self.app.get('/client/list', follow_redirects=True)
            self.assertEqual(response.status_code, 200)
            #checks to make sure there is no data in table since data is between <td>
            self.assertNotIn('<td>', str(response.data))
            self.assertNotIn('</td>', str(response.data))


if __name__ == "__main__":
    unittest.main()

在mcc.py中:

from flasc_mcc.models import Client
from flask import Flask, request, make_response, jsonify, Blueprint, flash, g, redirect, render_template, url_for, session, Response, current_app
from flask_login import login_required, current_user

bp = Blueprint('mcc', __name__)

@bp.route('/', methods=['GET','POST'])
@login_required
def index():
    return render_template('homepage.html')

@bp.route('/client/list', methods=['GET','POST'])
@login_required
def client_list():
    clients = Client.query.filter(Client.active==True).all()
    return render_template('mcc/client/client_list.html', clients=clients)

我正在从init.py提取应用程序,并在其中设置应用程序。 为什么找不到/ client / list的任何原因?我的想法也许是它与应用程序相关的事情。

0 个答案:

没有答案