Created
December 11, 2020 10:34
-
-
Save xbns/57b0257e5b0b577c8a160157ffc322a3 to your computer and use it in GitHub Desktop.
[simple test] example test using pytest_server,unittest and requests libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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