Search code examples
pythonpython-3.xpython-requestspython-unittesttrello

How to write a unittest for POST function in Python (Trello)?


I wanted to test a simple function like this, called '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()

I created a file called 'posting_test.py', I know I have to import post.py, but after that I'm stuck. How can I test the response status code and assert that it's 200?

import unittest
from posting import post

Solution

  • Very generally:

    class Mytest(unittest.TestCase):
    
        def test_post(self):
            response = post()
            self.assertEqual(response.status_code, 200)