I’m writing python tests with unittest and requests modules but getting AssertionError: <Response [200]> != 200
Tests are setup in two functions, test_get and test_post. Test runners starts from Tests class, where the issue is within test2. I’ve tried to assert this also: <Response [200]>
also. But getting this Error instead:
<Response [200]> != <Response [200]> Expected :<Response [200]> Actual :<Response [200]>
For this I am using httpbin and pycharm.
import requests import unittest # Test 1: Assert get data body get_url = 'https://httpbin.org/get' test_get = { 'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.21.0'}, 'origin': '94.255.130.105, 94.255.130.105', 'url': 'https://httpbin.org/get' } def get_httpbin(get_url): r = requests.get(get_url).json() return r # Test 2: Assert post is OK 200 post_url = 'https://httpbin.org/post' test_post = { 'sender': 'Alice', 'receiver': 'Bob', 'message': 'We did it!' } def post_httpbin(post_url): r = requests.post(post_url, test_post) return r # Test Runner class Tests(unittest.TestCase): def test1(self): self.assertEqual(get_httpbin(get_url), test_get) def test2(self): self.assertEqual(post_httpbin(post_url), 200) if __name__ == '__main__': unittest.main()
Advertisement
Answer
You’re comparing a response object with a number. They’re not equal.
What you intend is to compare the status code from the response object with a number. Try this:
def test2(self): self.assertEqual(post_httpbin(post_url).status_code, 200)