Amazing-Python-Scripts
146 строк · 13.4 Кб
1# A sample python code to automate the testing of the sample class having basic functionalities of to perform the mathematical operations on the specified operands of that particular function.
2
3# All the required modules which are required across the program are included at the beginning of the code.
4import unittest5import sys6
7A class is written which has different functions to perform the different mathematical operations depending upon the input provided the mathematical operation is performed on the two operands which are taken as input for each function, the class which is written below is the child class of the test case class from the unit test module, with the help of this parenting test case class we were able to use various inbuilt functions of the test case class which help us to write various test case function to test the mathematical results which are calculated by the various function written inside this class8
9
10class BasicMathOperationsTesting(the unit test.TestCase):11# Constructor is written which can be used to initialize the various class variable that is required to use throughout this class a variable that is declared inside the constructor of a class can be used across the scope of that particular class12def __init__(self):13pass14
15
16# A function is written in which the user is asked for two numbers on which the addition operation is performed, once the user provides the two numbers on which the addition operation is need to perform, that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable and that resultant variable is returned as the return value of this function, and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class
17
18def perform_addition(self):19operation_name = "addition"20print("Enter the first number for {} operation".format(operation_name))21num_1 = int(input())22print("Enter the second number for {} operation".format(operation_name))23num_2 = int(input())24
25resultant = num_1 + num_226return resultant27
28# A function is written in which the user is asked for two numbers on which the subtraction operation is performed, once the user provides the two numbers on which the subtraction operation is need to perform, that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable and that resultant variable is returned as the return value of this function, and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class
29def perform_subtraction(self):30operation_name = "subtraction"31print("Enter the first number for {} operation".format(operation_name))32num_1 = int(input())33print("Enter the second number for {} operation".format(operation_name))34num_2 = int(input())35
36resultant = num_1 - num_237return resultant38# A function is written in which the user is asked for two numbers on which the multiplication operation is performed, once the user provides the two numbers on which the multiplication operation is needed to perform, that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable and that resultant variable is returned as the return value of this function, and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class
39
40def perform_multiplication(self):41operation_name = "multiplication"42print("Enter the first number for {} operation".format(operation_name))43num_1 = int(input())44print("Enter the second number for {} operation".format(operation_name))45num_2 = int(input())46
47resultant = num_1 + num_248return resultant49
50# A function is written in which the user is asked for two numbers on which the division operation is performed, once the user provides the two numbers on which the division operation is needed to perform, that particular operation is performed on the provided two numbers and the result of that operation is stored in a resultant variable and that resultant variable is returned as the return value of this function, and then this return value can be used in various test function which will validate or test the particular operation performed by this function is correct or not by using various inbuilt methods of the test case class
51def perform_division(self):52operation_name = "division"53print("Enter the first number for {} operation".format(operation_name))54num_1 = int(input())55print("Enter the second number for {} operation".format(operation_name))56num_2 = int(input())57
58resultant = num_1 / num_259return resultant60
61# This is a test function which is written for testing the assert equal values which means this test case will be fast if the two values which are passed as parameters to this function are equal, so we have used this assert equal functions to test the various mathematical operations which are performed using the various function which is written above so, for example, let's say perform the addition operation of two variables which are passed as a parameter to that function so to verify that the addition operation performed that particular function is correct or not is then with the help of this function in which first parameter we passed as the actual value will be the value which is the value which is calculated by the function which is written above and the expected value is the value which is expected to be matched with this function so so if both of these values matches then they assert equal test case will be passed on the other hand if the both of these values are not equal then the assert equal test case will fail
62def the unit test_for_assert_equals(self, actual_value, expected_value):63
64actual_value_for_equals = actual_value65expected_value_for_equals = expected_value66self.assertEqual(actual_value_for_equals, expected_value_for_equals)67
68
69# This is another unit test case which we have written named assert true that means this function will return require a Boolean value and depending upon the value of the that boolean variable the nature of the resultant test case is dertemined so in this function we are using the assert false functionality of the unit test case class to test the actual and expected output of a mathematical operations so we are passing the oeprand one as the actual value by actual value we mean the value which is actually calculated by the any of the above written mathematical operation let's say multiplication so the resultant of the multiplication function is passed as the first operand to this function and the second operand which is passed to this function is the expected value or which is meant to be the actual output of that function and both of these operations are compared and stored into a variable since the comparison is done on two operands return type is a Boolean value show the variable storing those values will be a Boolean variable and that Boolean variable will be passed to the assert false function if that Boolean variable is having the true value then the test function will be passed on the other hand if the value is fasle the assert true unit test case will be failed
70
71def the unit test_for_assert_true(self, operand1, operand2):72
73boolean_resultant = operand1 == operand274self.assertTrue(boolean_resultant)75
76
77# This is another unit test case which we have written named assert false that means this function will return require a Boolean value and depending upon the value of the that boolean variable the nature of the resultant test case is dertemined so in this function we are using the assert false functionality of the unit test case class to test the actual and expected output of a mathematical operations so we are passing the oeprand one as the actual value by actual value we mean the value which is actually calculated by the any of the above written mathematical operation let's say multiplication so the resultant of the multiplication function is passed as the first operand to this function and the second operand which is passed to this function is the expected value or which is meant to be the actual output of that function and both of these operations are compared and stored into a variable since the comparison is done on two operands return type is a Boolean value show the variable storing those values will be a Boolean variable and that Boolean variable will be passed to the assert false function if that Boolean variable is having the fasle value then the test function will be passed on the other hand if the value is true the assert fasle unit test case will be failed
78
79def the unit test_for_assert_false(self, operand1, operand2):80
81boolean_resultant = operand1 != operand282self.assertFalse(boolean_resultant)83
84
85# In The End the main function is written, which has the object of the above-written class Which is used to call all the methods which are written inside the class. the user is provided with the list of menus from which he has to select the mathematical operation which he performs on the two inputs which are going he is going to provide after selecting the appropriate mathematical operation the result of that mathematical operation is presented to the user and another menu is printed from which the type of unit test which needs to be Run on that obtained result is shown, on selecting the appropriate unit test which will run on the result of the mathematical operation that particular test case is Run and depending upon the unit test which is selected by the user appropriate message is shown, that means if the test case is passed it is shown that that particular test case has been passed successfully on the other hand if the test case is failed due to some exception or error that has been encountered during the execution of that particular unit test case, then that particular exception or error message is printed to the user and which line has caused that an exception message is also presented to the user this helps in the debugging so that user can understand by which particular line of code that particular unit test is failing this printing of the menu is done in a recursive manner until the user exits the code execution by selecting the last option which is to exit the code execution.
86def main():87
88run_the unit test = BasicMathOperationsTesting()89
90while (True):91
92# from the listed below the list of operations select any one of the operations93print("Select any of the mathematical operations which are listed below:")94print("1. To perform the addition operation and then perform the unit test on the result obtained.")95print("2. To perform the subtraction operation and then perform the unit test on the result obtained.")96print("3. To perform the multiplication operation and then perform the unit test on the result obtained.")97print("4. To perform the division operation and then perform the unit test on the result obtained.")98print("5. To exit from the code execution.")99
100menu_choice = input()101menu_choice = int(menu_choice)102
103if menu_choice == 1:104result = run_the unit test.perform_addition()105elif menu_choice == 2:106result = run_the unit test.perform_subtraction()107elif menu_choice == 3:108result = run_the unit test.perform_multiplication()109elif menu_choice == 4:110result = run_the unit test.perform_division()111elif menu_choice == 5:112sys.exit()113
114print("Select any of the unit tests to perform which are listed below:")115print("1. To perform the assertEqual the unit test on the above done mathematical operation.")116print("2. To perform the assertTrue the unit test on the above done mathematical operation.")117print("3. To perform the assertFalse the unit test on the above done mathematical operation.")118
119menu_choice_for_unitttest = input()120menu_choice_for_unitttest = int(menu_choice_for_unitttest)121
122if menu_choice_for_unitttest == 1:123print("Expected value for test to pass:")124expected_value = int(input())125run_the unit test.the unit test_for_assert_equals(result, expected_value)126elif menu_choice_for_unitttest == 2:127print("Expected value for test to pass:")128expected_value = int(input())129run_the unit test.the unit test_for_assert_true(result, expected_value)130elif menu_choice_for_unitttest == 3:131print("Expected value for test to pass:")132expected_value = int(input())133run_the unit test.the unit test_for_assert_false(result, expected_value)134
135print(136"To go on with the code getting executed, enter input [y] or [n]")137continue_or_exit = input()138
139if continue_or_exit == 'y' or continue_or_exit == 'Y':140pass141elif continue_or_exit == 'n' or continue_or_exit == 'N':142sys.exit()143
144
145if __name__ == '__main__':146main()147