├── .gitignore ├── LICENSE ├── README.md ├── exampleTest.py ├── provoj-example.gif ├── provoj └── __init__.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | *.pyc 3 | provoj.egg-info 4 | dist 5 | build 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 arnau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # provoj 2 | 3 | Simple library to test the endpoints of an RESTful API. 4 | 5 | Check values, integrated with requests python library. 6 | 7 | For python version>3 8 | 9 | ## Installation 10 | 11 | Requires to have installed 'requests' library. 12 | 13 | Install provoj by: 14 | ``` 15 | pip install provoj 16 | ``` 17 | 18 | ### Usage 19 | Import the provoj library, and the requests library 20 | ```python 21 | import provoj 22 | import requests 23 | ``` 24 | 25 | Define a new test 26 | ```python 27 | test = provoj.NewTest("Testing some websites") 28 | ``` 29 | 30 | Check if two variables are equal 31 | ```python 32 | a = "hello" 33 | b = "world" 34 | test.equal("comparing a and b", a, b)# test.equal(check description, variable1, variable2) 35 | ``` 36 | 37 | Check if two variables are not equal 38 | ```python 39 | a = "hello" 40 | b = "world" 41 | test.notequal("comparing a and b", a, b)# test.notequal(check description, variable1, variable2) 42 | ``` 43 | 44 | Check if first variable is bigger than the second 45 | ```python 46 | a = 2 47 | b = 3 48 | test.bigger("comparing a and b", a, b)# test.bigger(check description, variable1, variable2) 49 | ``` 50 | 51 | Check if first variable is smaller than the second 52 | ```python 53 | a = 2 54 | b = 3 55 | test.smaller("comparing a and b", a, b)# test.smaller(check description, variable1, variable2) 56 | ``` 57 | 58 | Check if the length of a variable 59 | ```python 60 | animals = ["cat", "dog", "racoon"] 61 | test.length("checking the length of the array animals", animals, 3)# test.length(check description, variable1, length) 62 | ``` 63 | 64 | Check for request status 65 | ```python 66 | r = requests.get("https://api.github.com/users/arnaucode/repos") 67 | test.rStatus("get api.github.com/users/arnaucode/repos", r)# test.rStatus(check description, request_response) 68 | ``` 69 | 70 | ### Full example 71 | ```python 72 | import provoj 73 | import requests 74 | 75 | 76 | test1 = provoj.NewTest("testing some function") 77 | 78 | a = "hello" 79 | b = "world" 80 | c = "hello" 81 | test1.equal("comparing a and b", a, b) 82 | test1.notequal("comparing a and b", a, b) 83 | test1.equal("comparing a and c", a, c) 84 | 85 | x = 2 86 | y = 3 87 | test1.bigger("comparing x and y", x, y) 88 | test1.smaller("comparing x and y", x, y) 89 | 90 | test1.length("checking length", a, 2) 91 | test1.length("checking length", a, 5) 92 | 93 | animals = ["cat", "dog", "racoon"] 94 | test1.length("checking the length of the array animals", animals, 3) 95 | 96 | test1.printScores() 97 | 98 | 99 | t2 = provoj.NewTest("Testing some websites") 100 | 101 | r = requests.get("https://www.github.com") 102 | t2.rStatus("get github", r) 103 | 104 | r = requests.get("https://arnaucode.com") 105 | t2.rStatus("get arnaucode.com", r) 106 | 107 | r = requests.get("https://arnaucode.com/fake") 108 | t2.rStatus("get arnaucode.com/fake_path", r) 109 | 110 | r = requests.get("https://arnaucode.com/blog") 111 | t2.rStatus("get arnaucode.com/blog", r) 112 | 113 | t2.printScores() 114 | 115 | 116 | tGithub = provoj.NewTest("Github API tests") 117 | 118 | r = requests.get("https://api.github.com") 119 | tGithub.rStatus("get api.github", r) 120 | r = requests.get("https://api.github.com/users/arnaucode") 121 | tGithub.rStatus("get https://api.github.com/users/arnaucode", r) 122 | jsonResp = r.json() 123 | tGithub.equal("checking quantity of followers", jsonResp["followers"], 100) 124 | tGithub.equal("checking quantity of public_repos", jsonResp["public_repos"], 77) 125 | 126 | r = requests.get("https://api.github.com/users/arnaucode/repos") 127 | tGithub.rStatus("get https://api.github.com/users/arnaucode/repos", r) 128 | jsonResp = r.json() 129 | tGithub.length("checking length of repos", jsonResp, 30) 130 | tGithub.equal("checking first repo", jsonResp[0]["name"], "argos") 131 | tGithub.equal("checking second repo", jsonResp[1]["name"], "coffeeminer") 132 | tGithub.equal("checking third repo", jsonResp[2]["name"], "bc") 133 | 134 | tGithub.printScores() 135 | ``` 136 | 137 | Output example: 138 | 139 | ![provoj](https://raw.githubusercontent.com/arnaucode/provoj/master/provoj-example.gif "provoj") 140 | -------------------------------------------------------------------------------- /exampleTest.py: -------------------------------------------------------------------------------- 1 | import provoj 2 | import requests 3 | 4 | 5 | test1 = provoj.NewTest("testing some function") 6 | 7 | a = "hello" 8 | b = "world" 9 | c = "hello" 10 | 11 | test1.equal("comparing a and b", a, b) 12 | test1.notequal("comparing a and b", a, b) 13 | 14 | test1.equal("comparing a and c", a, c) 15 | 16 | x = 2 17 | y = 3 18 | test1.bigger("comparing x and y", x, y) 19 | test1.smaller("comparing x and y", x, y) 20 | 21 | test1.length("checking length", a, 2) 22 | test1.length("checking length", a, 5) 23 | 24 | animals = ["cat", "dog", "racoon"] 25 | test1.length("checking the length of the array animals", animals, 3) 26 | 27 | test1.printScores() 28 | 29 | 30 | t2 = provoj.NewTest("Testing some websites") 31 | 32 | r = requests.get("https://www.github.com") 33 | t2.rStatus("get github", r) 34 | 35 | r = requests.get("https://arnaucode.com") 36 | t2.rStatus("get arnaucode.com", r) 37 | 38 | r = requests.get("https://arnaucode.com/fake") 39 | t2.rStatus("get arnaucode.com/fake_path", r) 40 | 41 | r = requests.get("https://arnaucode.com/blog") 42 | t2.rStatus("get arnaucode.com/blog", r) 43 | 44 | t2.printScores() 45 | 46 | 47 | tGithub = provoj.NewTest("Github API tests") 48 | 49 | r = requests.get("https://api.github.com") 50 | tGithub.rStatus("get api.github", r) 51 | r = requests.get("https://api.github.com/users/arnaucode") 52 | tGithub.rStatus("get https://api.github.com/users/arnaucode", r) 53 | jsonResp = r.json() 54 | tGithub.equal("checking quantity of followers", jsonResp["followers"], 100) 55 | tGithub.equal("checking quantity of public_repos", jsonResp["public_repos"], 77) 56 | 57 | r = requests.get("https://api.github.com/users/arnaucode/repos") 58 | tGithub.rStatus("get https://api.github.com/users/arnaucode/repos", r) 59 | jsonResp = r.json() 60 | tGithub.length("checking length of repos", jsonResp, 30) 61 | tGithub.equal("checking first repo", jsonResp[0]["name"], "argos") 62 | tGithub.equal("checking second repo", jsonResp[1]["name"], "coffeeminer") 63 | tGithub.equal("checking third repo", jsonResp[2]["name"], "bc") 64 | 65 | tGithub.printScores() 66 | -------------------------------------------------------------------------------- /provoj-example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arnaucube/provoj/f25e8ceedaee7837a075f003a9ac9c94d1244063/provoj-example.gif -------------------------------------------------------------------------------- /provoj/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import math 3 | import datetime 4 | 5 | class NewTest(object): 6 | def __init__(self, title=None): 7 | # define the scores dictionary, and initialize 8 | self.scores = {} 9 | self.scores["success"] = 0 10 | self.scores["error"] = 0 11 | self.time = datetime.datetime.now() 12 | if title != None: 13 | self.title = title 14 | print("") 15 | pc.blue("- " + title) 16 | else: 17 | self.title = "" 18 | print("") 19 | pc.blue("- unnamed test") 20 | def equal(self, action, a, b): 21 | if a != b: 22 | pc.error("{} - Error, {} should be equal to {}".format(action, a, b)) 23 | self.scores["error"] += 1 24 | else: 25 | pc.checked(action) 26 | self.scores["success"] += 1 27 | def notequal(self, action, a, b): 28 | if a == b: 29 | pc.error("{} - Error, {} should be not equal to {}".format(action, a, b)) 30 | self.scores["error"] += 1 31 | else: 32 | pc.checked(action) 33 | self.scores["success"] += 1 34 | def bigger(self, action, a, b): 35 | if a < b: 36 | pc.error("{} - Error, {} should be bigger than {}".format(action, a, b)) 37 | self.scores["error"] += 1 38 | else: 39 | pc.checked(action) 40 | self.scores["success"] += 1 41 | def smaller(self, action, a, b): 42 | if a > b: 43 | pc.error("{} - Error, {} should be smaller than {}".format(action, a, b)) 44 | self.scores["error"] += 1 45 | else: 46 | pc.checked(action) 47 | self.scores["success"] += 1 48 | 49 | def length(self, action, a, l): 50 | if len(a) != l: 51 | pc.error("{} - Error, actual length is {}, expected to be {}".format(action, a, l)) 52 | self.scores["error"] += 1 53 | else: 54 | pc.checked(action) 55 | self.scores["success"] += 1 56 | 57 | def rStatus(self, action, r, verbose=False): 58 | if r.status_code < 200 or r.status_code > 299: 59 | if verbose: 60 | pc.error("{} - Error {}: {}".format(action, r.status_code, r.text)) 61 | else: 62 | pc.error("{} - Error {}".format(action, r.status_code)) 63 | 64 | self.scores["error"] += 1 65 | else: 66 | pc.checked(action) 67 | self.scores["success"] += 1 68 | 69 | def printScores(self): 70 | pc.purple(" Test title: " + self.title) 71 | success = self.scores["success"] 72 | error = self.scores["error"] 73 | score = math.ceil((success/(success+error))*100) 74 | elapsed = datetime.datetime.now() - self.time 75 | pc.purple(" Score: {} % ({}/{}) Time benchmark: {}".format(score, success, success+error, elapsed)) 76 | 77 | 78 | ''' 79 | code to print the colors in the terminal 80 | ''' 81 | checkIcon = "✓ " 82 | errorIcon = "❌ " 83 | 84 | class pc: 85 | RED = '\033[31m' 86 | GREEN = '\033[32m' 87 | BLUE = '\033[94m' 88 | PURPLE = '\033[95m' 89 | END = '\033[0m' 90 | 91 | @classmethod 92 | def red(cls, s, **kwargs): 93 | print(cls.RED + s + cls.END, **kwargs) 94 | 95 | @classmethod 96 | def green(cls, s, **kwargs): 97 | print(cls.GREEN + s + cls.END, **kwargs) 98 | 99 | @classmethod 100 | def blue(cls, s, **kwargs): 101 | print(cls.BLUE + s + cls.END, **kwargs) 102 | 103 | @classmethod 104 | def purple(cls, s, **kwargs): 105 | print(cls.PURPLE + s + cls.END, **kwargs) 106 | 107 | @classmethod 108 | def checked(cls, s, **kwargs): 109 | print(" " + cls.GREEN + checkIcon + s + cls.END, **kwargs) 110 | 111 | @classmethod 112 | def error(cls, s, **kwargs): 113 | print(" " + cls.RED + errorIcon + s + cls.END, **kwargs) 114 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup( 4 | name='provoj', 5 | packages=['provoj'], 6 | description='Simple library to test the endpoints of an RESTful API.', 7 | url='https://github.com/arnaucode/provoj', 8 | author='arnaucode', 9 | author_email='arnaucode@gmail.com', 10 | version='0.0.0.1', 11 | scripts=['provoj/__init__.py'] 12 | ) 13 | --------------------------------------------------------------------------------