-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_my_api.py
64 lines (51 loc) · 2.08 KB
/
test_my_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import unittest
from app import create_app
import cProfile
import pstats
import io
BOLD = '\033[1m'
END = '\033[0m'
class MyTestCase(unittest.TestCase):
# More test: https://medium.com/grammofy/testing-your-python-api-app-with-json-schema-52677fe73351
def setUp(self):
self.app = create_app()
self.client = self.app.test_client()
self.methods = {"get": self.client.get, "post": self.client.get}
def make_profile_endpoint(self, endpoint: str, method: str):
header_fill_char = "-"
body_fill_char = "#"
title_size = 60
subtitle_size = 5
call_endpoint = self.methods.get(method, None)
if not call_endpoint:
raise Exception("Method not supported!")
pr = cProfile.Profile()
pr.enable()
# Start
call_endpoint(endpoint)
# End
pr.disable()
ps = pstats.Stats(pr, stream=io.StringIO()).sort_stats()
# pr.print_stats()
print("-".center(title_size, header_fill_char))
print(" Deterministic Profiling ({}) ".center(title_size, body_fill_char).format(endpoint))
print("-".center(title_size, header_fill_char))
print("{}EndPoint:{} {}".center(subtitle_size).format(BOLD, END, endpoint))
print("{}Total calls:{} {}".center(subtitle_size).format(BOLD, END, ps.total_calls))
print("{}Total TT:{} {}\n".center(subtitle_size).format(BOLD, END, ps.total_tt))
def test_main_is_json(self):
res = self.client.get("/")
self.assertEqual(res.status_code, 200)
self.assertEqual(res.is_json, False)
self.assertIn("The API is running..", str(res.data))
def test_endpoint_hello(self):
res = self.client.get("/hello/", content_type="application/json")
self.assertEqual(res.status_code, 200)
self.assertEqual(res.is_json, True)
json_data = res.json
assert 'message' in json_data
def test_endpoint_time(self):
self.make_profile_endpoint("/hello", "get")
self.make_profile_endpoint("/", "get")
if __name__ == '__main__':
unittest.main()