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:
JavaScript
x
5
1
<Response [200]> != <Response [200]>
2
3
Expected :<Response [200]>
4
Actual :<Response [200]>
5
For this I am using httpbin and pycharm.
JavaScript
1
44
44
1
import requests
2
import unittest
3
4
# Test 1: Assert get data body
5
get_url = 'https://httpbin.org/get'
6
test_get =
7
{
8
'args': {},
9
'headers': {'Accept': '*/*',
10
'Accept-Encoding': 'gzip, deflate',
11
'Host': 'httpbin.org',
12
'User-Agent': 'python-requests/2.21.0'},
13
'origin': '94.255.130.105, 94.255.130.105',
14
'url': 'https://httpbin.org/get'
15
}
16
17
def get_httpbin(get_url):
18
r = requests.get(get_url).json()
19
return r
20
21
# Test 2: Assert post is OK 200
22
post_url = 'https://httpbin.org/post'
23
test_post =
24
{
25
'sender': 'Alice',
26
'receiver': 'Bob',
27
'message': 'We did it!'
28
}
29
30
def post_httpbin(post_url):
31
r = requests.post(post_url, test_post)
32
return r
33
34
# Test Runner
35
class Tests(unittest.TestCase):
36
def test1(self):
37
self.assertEqual(get_httpbin(get_url), test_get)
38
39
def test2(self):
40
self.assertEqual(post_httpbin(post_url), 200)
41
42
if __name__ == '__main__':
43
unittest.main()
44
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:
JavaScript
1
3
1
def test2(self):
2
self.assertEqual(post_httpbin(post_url).status_code, 200)
3