From 7e4e35f597a54ccb4151e2574e347efdb6ef56e5 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 19 Mar 2024 00:30:25 +0100 Subject: [PATCH 1/2] Silencing python testing, optional verbose output --- testing.py | 67 +++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/testing.py b/testing.py index 1eea03b..71b614e 100644 --- a/testing.py +++ b/testing.py @@ -2,6 +2,11 @@ import requests import string import random +debug_output = False + +def dprint(*args, **kwargs): + if debug_output: + print(*args, **kwargs) def randomString(len=10): """Generate a random string of fixed length""" @@ -31,7 +36,7 @@ getUserProjectsPath = base_url + "/api/getUserProjects" def test_get_user_projects(): - print("Testing get user projects") + dprint("Testing get user projects") loginResponse = login("user2", "123") # Check if the user is added to the project response = requests.get( @@ -39,29 +44,29 @@ def test_get_user_projects(): json={"username": "user2"}, headers={"Authorization": "Bearer " + loginResponse.json()["token"]}, ) - print(response.text) - print(response.json()) + dprint(response.text) + dprint(response.json()) assert response.status_code == 200, "Get user projects failed" - print("got user projects successfully") + dprint("got user projects successfully") # Posts the username and password to the register endpoint def register(username: string, password: string): - print("Registering with username: ", username, " and password: ", password) + dprint("Registering with username: ", username, " and password: ", password) response = requests.post( registerPath, json={"username": username, "password": password} ) - print(response.text) + dprint(response.text) return response # Posts the username and password to the login endpoint def login(username: string, password: string): - print("Logging in with username: ", username, " and password: ", password) + dprint("Logging in with username: ", username, " and password: ", password) response = requests.post( loginPath, json={"username": username, "password": password} ) - print(response.text) + dprint(response.text) return response @@ -69,7 +74,7 @@ def login(username: string, password: string): def test_login(): response = login(username, "always_same") assert response.status_code == 200, "Login failed" - print("Login successful") + dprint("Login successful") return response.json()["token"] @@ -77,8 +82,7 @@ def test_login(): def test_create_user(): response = register(username, "always_same") assert response.status_code == 200, "Registration failed" - print("Registration successful") - + dprint("Registration successful") # Test function to add a project def test_add_project(): @@ -89,10 +93,9 @@ def test_add_project(): json={"name": projectName, "description": "This is a project"}, headers={"Authorization": "Bearer " + token}, ) - print(response.text) + dprint(response.text) assert response.status_code == 200, "Add project failed" - print("Add project successful") - + dprint("Add project successful") # Test function to submit a report def test_submit_report(): @@ -111,10 +114,9 @@ def test_submit_report(): }, headers={"Authorization": "Bearer " + token}, ) - print(response.text) + dprint(response.text) assert response.status_code == 200, "Submit report failed" - print("Submit report successful") - + dprint("Submit report successful") # Test function to get a weekly report def test_get_weekly_report(): @@ -124,7 +126,7 @@ def test_get_weekly_report(): headers={"Authorization": "Bearer " + token}, params={"username": username, "projectName": projectName, "week": 1}, ) - print(response.text) + dprint(response.text) assert response.status_code == 200, "Get weekly report failed" @@ -135,7 +137,7 @@ def test_get_project(): getProjectPath + "/1", # Assumes that the project with id 1 exists headers={"Authorization": "Bearer " + token}, ) - print(response.text) + dprint(response.text) assert response.status_code == 200, "Get project failed" @@ -144,13 +146,13 @@ def test_add_user_to_project(): # Log in as a site admin admin_username = randomString() admin_password = "admin_password" - print( + dprint( "Registering with username: ", admin_username, " and password: ", admin_password ) response = requests.post( registerPath, json={"username": admin_username, "password": admin_password} ) - print(response.text) + dprint(response.text) admin_token = login(admin_username, admin_password).json()["token"] response = requests.post( @@ -158,9 +160,9 @@ def test_add_user_to_project(): json={"username": admin_username}, headers={"Authorization": "Bearer " + admin_token}, ) - print(response.text) + dprint(response.text) assert response.status_code == 200, "Promote to site admin failed" - print("Admin promoted to site admin successfully") + dprint("Admin promoted to site admin successfully") # Create a new user to add to the project new_user = randomString() @@ -173,10 +175,9 @@ def test_add_user_to_project(): headers={"Authorization": "Bearer " + admin_token}, ) - print(response.text) + dprint(response.text) assert response.status_code == 200, "Add user to project failed" - print("Add user to project successful") - + dprint("Add user to project successful") # Test function to sign a report def test_sign_report(): @@ -187,13 +188,13 @@ def test_sign_report(): # Register an admin admin_username = randomString() admin_password = "admin_password2" - print( + dprint( "Registering with username: ", admin_username, " and password: ", admin_password ) response = requests.post( registerPath, json={"username": admin_username, "password": admin_password} ) - print(response.text) + dprint(response.text) # Log in as the admin admin_token = login(admin_username, admin_password).json()["token"] @@ -213,7 +214,7 @@ def test_sign_report(): headers={"Authorization": "Bearer " + admin_token}, ) assert response.status_code == 200, "Add project manager to project failed" - print("Project manager added to project successfully") + dprint("Project manager added to project successfully") # Log in as the project manager project_manager_token = login(project_manager, "project_manager_password").json()[ @@ -237,7 +238,7 @@ def test_sign_report(): headers={"Authorization": "Bearer " + token}, ) assert response.status_code == 200, "Submit report failed" - print("Submit report successful") + dprint("Submit report successful") # Retrieve the report ID response = requests.get( @@ -245,7 +246,7 @@ def test_sign_report(): headers={"Authorization": "Bearer " + token}, params={"username": username, "projectName": projectName, "week": 1}, ) - print(response.text) + dprint(response.text) report_id = response.json()["reportId"] # Sign the report as the project manager @@ -255,7 +256,7 @@ def test_sign_report(): headers={"Authorization": "Bearer " + project_manager_token}, ) assert response.status_code == 200, "Sign report failed" - print("Sign report successful") + dprint("Sign report successful") # Retrieve the report ID again for confirmation response = requests.get( @@ -263,7 +264,7 @@ def test_sign_report(): headers={"Authorization": "Bearer " + token}, params={"username": username, "projectName": projectName, "week": 1}, ) - print(response.text) + dprint(response.text) if __name__ == "__main__": From 5bcca0202b1c6ed1285ee86a6e1bc56c62fa9fc7 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 19 Mar 2024 01:12:14 +0100 Subject: [PATCH 2/2] Better test feedback in python script --- testing.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/testing.py b/testing.py index 71b614e..f4666a7 100644 --- a/testing.py +++ b/testing.py @@ -4,6 +4,11 @@ import random debug_output = False +def gprint(*args, **kwargs): + print("\033[92m", *args, "\033[00m", **kwargs) + +print("Running Tests...") + def dprint(*args, **kwargs): if debug_output: print(*args, **kwargs) @@ -47,7 +52,7 @@ def test_get_user_projects(): dprint(response.text) dprint(response.json()) assert response.status_code == 200, "Get user projects failed" - dprint("got user projects successfully") + gprint("test_get_user_projects successful") # Posts the username and password to the register endpoint @@ -75,6 +80,7 @@ def test_login(): response = login(username, "always_same") assert response.status_code == 200, "Login failed" dprint("Login successful") + gprint("test_login successful") return response.json()["token"] @@ -82,7 +88,7 @@ def test_login(): def test_create_user(): response = register(username, "always_same") assert response.status_code == 200, "Registration failed" - dprint("Registration successful") + gprint("test_create_user successful") # Test function to add a project def test_add_project(): @@ -95,7 +101,7 @@ def test_add_project(): ) dprint(response.text) assert response.status_code == 200, "Add project failed" - dprint("Add project successful") + gprint("test_add_project successful") # Test function to submit a report def test_submit_report(): @@ -116,7 +122,7 @@ def test_submit_report(): ) dprint(response.text) assert response.status_code == 200, "Submit report failed" - dprint("Submit report successful") + gprint("test_submit_report successful") # Test function to get a weekly report def test_get_weekly_report(): @@ -128,6 +134,7 @@ def test_get_weekly_report(): ) dprint(response.text) assert response.status_code == 200, "Get weekly report failed" + gprint("test_get_weekly_report successful") # Tests getting a project by id @@ -139,6 +146,7 @@ def test_get_project(): ) dprint(response.text) assert response.status_code == 200, "Get project failed" + gprint("test_get_project successful") # Test function to add a user to a project @@ -177,7 +185,7 @@ def test_add_user_to_project(): dprint(response.text) assert response.status_code == 200, "Add user to project failed" - dprint("Add user to project successful") + gprint("test_add_user_to_project successful") # Test function to sign a report def test_sign_report(): @@ -265,6 +273,7 @@ def test_sign_report(): params={"username": username, "projectName": projectName, "week": 1}, ) dprint(response.text) + gprint("test_sign_report successful") if __name__ == "__main__":