Flask_testing对于看似有效的端点意外返回404

为我的烧瓶应用程序编写一些单元测试。当我尝试邮递员时,端点'/'有效并返回200,但是flask_testing给出AssertionError: 404 != 200

我已经设置了基本配置。

class BaseTestCase(TestCase):

    def create_app(self):
        app = flask(__name__)
        app.config['TESTING'] = True
        app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///:memory:"
        app.config['JWT_SECRET_KEY'] = environ['JWT_SECRET_KEY']
        db = SQLAlchemy(app)
        db.init_app(app)
        return app

这是测试。

class flaskTestCase(BaseTestCase):

    def test_root(self):
        response = self.client.get('/')
        self.assertEquals(response.status_code,200)

测试输出

======================================================================
FAIL: test_root (test.server-test.flaskTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/my_name/path/to/my_project/test/server-test.py",line 13,in test_root
    self.assertEquals(response.status_code,200)
AssertionError: 404 != 200

----------------------------------------------------------------------
Ran 1 test in 0.032s

FAILED (failures=1)
wsnfairy 回答:Flask_testing对于看似有效的端点意外返回404

就我而言,我在测试的路线中有这个 if 语句

if len(books_per_request) == 0:
   response = make_response(jsonify(message="value exceeded books count"),404)

虽然请求成功,但我在测试运行和计数 len == 0 时发送了 404 错误,如果这是像我这样的第一个测试,它会继续返回错误,如果一切正常,请检查返回正常字符串的正常路由,检查你的回家路线'/'

  1. 确保它存在于您的 init.py 文件中
  2. 并确保它不会返回任何 404 错误

希望对大家有帮助

本文链接:https://www.f2er.com/3127241.html

大家都在问