向Python unittest添加更高级的断言

时间:2017-06-11 10:07:46

标签: python rest api unit-testing python-unittest

我正在测试Trello API,创建和删除卡片。我在这里唯一的断言是状态代码。如何在下面的代码中添加更多“高级”断言?

import unittest

from post_and_delete import *


class TestBasic(unittest.TestCase):
    def test_post_delete(self):
        # create new card with random name
        name = nonce(10)

        result_post = post(name)
        self.assertEqual(result_post.json()['name'], name)
        self.assertEqual(result_post.status_code, 200)

        card_id = result_post.json()['id']
        # get the card, verify it exists, status code should be 200
        result_get = get(card_id)
        self.assertEqual(result_get.status_code, 200)

        # delete the card, check again if status code is 200
        result = delete(card_id)
        self.assertEqual(result.status_code, 200)
        # get the recently deleted card, status code should be 404
        result_get = get(card_id)
        self.assertEqual(result_get.status_code, 404)

        # try to delete the card again, status code should be 404
        result = delete(card_id)
        self.assertEqual(result.status_code, 404)


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

0 个答案:

没有答案