Skip to content

Instantly share code, notes, and snippets.

@xbns
Created December 11, 2020 10:34
Show Gist options
  • Save xbns/57b0257e5b0b577c8a160157ffc322a3 to your computer and use it in GitHub Desktop.
Save xbns/57b0257e5b0b577c8a160157ffc322a3 to your computer and use it in GitHub Desktop.
[simple test] example test using pytest_server,unittest and requests libraries
import requests
import unittest
from pytest_httpserver import HTTPServer
import json
class APITest(unittest.TestCase):
def test_my_client(self):
with HTTPServer() as httpserver:
# set up the server to serve "/" with the json
httpserver.expect_request('/content').respond_with_json({'result': 30})
# check that the request is served
response = requests.get(httpserver.url_for('/content')).json()
self.assertEqual(response, {"result":31})
if __name__ == "__main__":
unittest.main()
## Expected Output
#
# $ python simple-test.py
#127.0.0.1 - - [11/Dec/2020 13:04:35] "GET / HTTP/1.1" 200 -
#.
#----------------------------------------------------------------------
#Ran 1 test in 0.004s
#
#OK
#$
#$ python simple-test.py
#127.0.0.1 - - [11/Dec/2020 13:28:11] "GET /content HTTP/1.1" 200 -
#F
#======================================================================
#FAIL: test_my_client (__main__.APITest)
#----------------------------------------------------------------------
#Traceback (most recent call last):
# File "simple-test.py", line 13, in test_my_client
# self.assertEqual(response, {"result":31})
#AssertionError: {'result': 30} != {'result': 31}
#- {'result': 30}
#? ^
#
#+ {'result': 31}
#? ^
#
#
#----------------------------------------------------------------------
#Ran 1 test in 0.004s
#
#FAILED (failures=1)
#$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment