如何在Python(Trello)中为POST函数编写单元测试?

时间:2017-05-14 20:51:12

标签: python python-3.x python-requests python-unittest trello

我想测试一个这样的简单函数,名为" posting.py':

import requests

def post():
        url = "https://api.trello.com/1/cards"
        params = {"key": "my_key",
                  "token": "my_token",
                  "name": "random_name",
                  "idList": "id"}
        response = requests.post(url=url, params=params)
        return response
post()

我创建了一个名为' posting_test.py'的文件,我知道我必须导入post.py,但之后我就被卡住了。如何测试响应状态代码并声明它是200?

import unittest
from posting import post

1 个答案:

答案 0 :(得分:2)

非常普遍:

class Mytest(unittest.TestCase):

    def test_post(self):
        response = post()
        self.assertEqual(response.status_code, 200)