在测试Flask应用程序时保留多个功能的更改

时间:2016-07-18 13:20:28

标签: python testing flask pytest

我在关于创建API的Flask上发表演讲。我想为它写一些测试。在测试创建资源时是否测试在另一个函数中删除资源?如何使资源的创建持久化以进行删除和编辑测试?

David Baumgold - Prototyping New APIs with Flask - PyCon 2016

该演讲展示了如何为小狗的名字和图片网址制作API。

  • 您在索引页面上创建了一个带有POST请求的小狗
  • 你会得到一只来自' / puppy_name'
  • 的GET小狗
  • 您可以从' /'
  • 获取带有GET的小狗列表
  • 您使用PUT编辑了一只小狗' / puppy_name' (当然还有新数据)
  • 您从' / puppy_name'
  • 删除了带有DELETE的小狗
import py.test
import unittest
from requests import get, post, delete, put

localhost = 'http://localhost:5000'

class TestApi(unittest.TestCase):
    def test_list_puppies(self):
        index = get(localhost)
        assert index.status_code == 200

    def test_get_puppy(self):
        puppy1 = get(localhost + '/rover')
        puppy2 = get(localhost + '/spot')
        assert puppy1.status_code == 200 and puppy2.status_code == 200

    def test_create_puppy(self):
        create = post(localhost, data={
            'name': 'lassie', 'image_url': 'lassie_url'})
        assert create.status_code == 201

    @py.test.mark.skip('cannot fix it')
    def test_edit_puppy(self):
        ret = put(localhost + '/lassie',
                  data={'name': 'xxx', 'image_url': 'yyy'})
        assert ret.status_code == 201

    def test_puppy_exits(self):
        lassie = get(localhost + '/lassie').status_code
        assert lassie == 200

    def test_delete_puppy(self):
        ret = delete(localhost + '/lassie')
        assert ret.status_code == 200
@app.route('/', methods=['POST'])
def create_puppy():
    puppy, errors = puppy_schema.load(request.form)
    if errors:
    response = jsonify(errors)
    response.status_code = 400
    return response

    puppy.slug = slugify(puppy.name)

    # create in database
    db.session.add(puppy)
    db.session.commit()

    # return an HTTP response
    response = jsonify( {'message': 'puppy created'} )
    response.status_code = 201
    location = url_for('get_puppy', slug=puppy.slug)
    response.headers['location'] = location

    return response

@app.route('/<slug>', methods=['DELETE'])
def delete_puppy(slug):
    puppy = Puppy.query.filter(Puppy.slug == slug).first_or_404()
    db.session.delete(puppy)
    db.session.commit()

    return jsonify( {'message': '{} deleted'.format(puppy.name)} )

&quot; test_edit_puppy&#39;中的断言语句和&#39; test_puppy_exists&#39;失败。我得到404状态代码而不是201和200。

1 个答案:

答案 0 :(得分:3)

你正在测试错误的东西。您可以将更改保留在数据库中,只需提交它,当您运行测试时,但您真的不想这样做。

通过单元测试,您可以测试简单的单元。通过集成测试,这就是您在此处讨论的内容,您仍然希望每个测试都有一个特定的焦点。在这种特殊情况下,您可能希望执行以下操作:

def test_delete_puppy(self):
    create = post(localhost, data={
        'name': 'lassie', 'image_url': 'lassie_url'})        
    lassie = get(localhost + '/lassie')

    # not sure if there's an "assume" method, but I would
    # use that here - the test is not a valid test
    # if you can't create a puppy and retrieve the puppy
    # then there's really no way to delete something that
    # does not exist
    assert create.status_code == 201
    assert lassie.status_code == 200

    ret = delete(localhost + '/lassie')
    assert ret.status_code == 200

    lassie = get(localhost + '/lassie')
    assert lassie.status_code == 404

此测试的重点是测试删除小狗是否正常。但是您希望将数据库中的小狗设置为测试的一部分或测试设置的一部分。它是arrange的{​​{1}}部分。在实际执行测试之前,您需要按照正确的顺序安排世界状态。集成测试和单元测试之间的区别在于,通过单元测试,您可以模拟您调用的所有端点,而使用集成测试,您可以实际设置所有数据。在运行测试部分之前需要。你想在这做什么。