首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写集成测试新手

编写集成测试新手
EN

Stack Overflow用户
提问于 2014-06-14 09:45:16
回答 1查看 1K关注 0票数 1

下面是我的测试文件:

代码语言:javascript
复制
from flask import Flask
from flask.ext.testing import TestCase


class TestInitViews(TestCase):

    render_templates = False

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True
        return app

    def test_root_route(self):
        self.client.get('/')
        self.assert_template_used('index.html')

下面是完整的堆栈跟踪:

代码语言:javascript
复制
$ nosetests tests/api/client/test_init_views.py
F
======================================================================
FAIL: test_root_route (tests.api.client.test_init_views.TestInitViews)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/dmonsewicz/dev/autoresponders/tests/api/client/test_init_views.py", line 17, in test_root_route
    self.assert_template_used('index.html')
  File "/Users/dmonsewicz/.virtualenvs/autoresponders-api/lib/python2.7/site-packages/flask_testing.py", line 120, in assertTemplateUsed
    raise AssertionError("template %s not used" % name)
AssertionError: template index.html not used

----------------------------------------------------------------------
Ran 1 test in 0.012s

FAILED (failures=1)

我是Python的新手,似乎不能理解这一点。我所要做的就是编写一个简单的测试,该测试命中/ (根路由)端点和asserts,所使用的模板实际上是index.html

尝试使用LiveServerTestCase

代码语言:javascript
复制
from flask import Flask
from flask.ext.testing import LiveServerTestCase


class TestInitViews(LiveServerTestCase):

    render_templates = False

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True
        app.config['LIVESERVER_PORT'] = 6765

        return app

    def setUp(self):
        self.app = self.app.test_client()

    def test_root_route(self):
        res = self.app.get('/')

        print(res)

        self.assert_template_used('index.html')

我使用的是Flask-Testing版本0.4,但由于某种原因,我的导入中不存在该LiveServerTestCase

工作代码

代码语言:javascript
复制
from flask import Flask
from flask.ext.testing import TestCase
from api.client import blueprint


class TestInitViews(TestCase):

    render_templates = False

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True

        app.register_blueprint(blueprint)

        return app

    def test_root_route(self):
        res = self.client.get('/')
        self.assert_template_used('index.html')
EN

回答 1

Stack Overflow用户

发布于 2014-06-14 10:05:53

你必须运行pip install blinker并确保你的flask版本高于.6。

看起来您忽略了设置app.config‘’TESTING‘= True

我能够运行以下测试来验证断言是否为True:

代码语言:javascript
复制
#!/usr/bin/python

import unittest
from flask import Flask
from flask.ext.testing import TestCase
from flask import render_template


class MyTest(TestCase):

  def create_app(self):
    app = Flask(__name__)
    app.config['TESTING'] = True

    @app.route('/')
    def hello_world():
      return render_template('index.html')
    return app

  def test_root_route(self):
    self.client.get('/')
    self.assert_template_used('index.html')

if __name__ == '__main__':
  unittest.main()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24215880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档