Amazing-Python-Scripts

Форк
0
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.
4
import unittest
5
import sys
6

7
A 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 class
8

9

10
class 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 class
12
    def __init__(self):
13
        pass
14

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

18
    def perform_addition(self):
19
        operation_name = "addition"
20
        print("Enter the first number for {} operation".format(operation_name))
21
        num_1 = int(input())
22
        print("Enter the second number for {} operation".format(operation_name))
23
        num_2 = int(input())
24

25
        resultant = num_1 + num_2
26
        return resultant
27

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
29
    def perform_subtraction(self):
30
        operation_name = "subtraction"
31
        print("Enter the first number for {} operation".format(operation_name))
32
        num_1 = int(input())
33
        print("Enter the second number for {} operation".format(operation_name))
34
        num_2 = int(input())
35

36
        resultant = num_1 - num_2
37
        return resultant
38
# 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

40
    def perform_multiplication(self):
41
        operation_name = "multiplication"
42
        print("Enter the first number for {} operation".format(operation_name))
43
        num_1 = int(input())
44
        print("Enter the second number for {} operation".format(operation_name))
45
        num_2 = int(input())
46

47
        resultant = num_1 + num_2
48
        return resultant
49

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
51
    def perform_division(self):
52
        operation_name = "division"
53
        print("Enter the first number for {} operation".format(operation_name))
54
        num_1 = int(input())
55
        print("Enter the second number for {} operation".format(operation_name))
56
        num_2 = int(input())
57

58
        resultant = num_1 / num_2
59
        return resultant
60

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
62
    def the unit test_for_assert_equals(self, actual_value, expected_value):
63

64
        actual_value_for_equals = actual_value
65
        expected_value_for_equals = expected_value
66
        self.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

71
    def the unit test_for_assert_true(self, operand1, operand2):
72

73
        boolean_resultant = operand1 == operand2
74
        self.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

79
    def the unit test_for_assert_false(self, operand1, operand2):
80

81
        boolean_resultant = operand1 != operand2
82
        self.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.
86
def main():
87

88
    run_the unit test = BasicMathOperationsTesting()
89

90
    while (True):
91

92
        # from the listed below the list of operations select any one of the operations
93
        print("Select any of the mathematical operations which are listed below:")
94
        print("1. To perform the addition operation and then perform the unit test on the result obtained.")
95
        print("2. To perform the subtraction operation and then perform the unit test on the result obtained.")
96
        print("3. To perform the multiplication operation and then perform the unit test on the result obtained.")
97
        print("4. To perform the division operation and then perform the unit test on the result obtained.")
98
        print("5. To exit from the code execution.")
99

100
        menu_choice = input()
101
        menu_choice = int(menu_choice)
102

103
        if menu_choice == 1:
104
            result = run_the unit test.perform_addition()
105
        elif menu_choice == 2:
106
            result = run_the unit test.perform_subtraction()
107
        elif menu_choice == 3:
108
            result = run_the unit test.perform_multiplication()
109
        elif menu_choice == 4:
110
            result = run_the unit test.perform_division()
111
        elif menu_choice == 5:
112
            sys.exit()
113

114
        print("Select any of the unit tests to perform which are listed below:")
115
        print("1. To perform the assertEqual the unit test on the above done mathematical operation.")
116
        print("2. To perform the assertTrue the unit test on the above done mathematical operation.")
117
        print("3. To perform the assertFalse the unit test on the above done mathematical operation.")
118

119
        menu_choice_for_unitttest = input()
120
        menu_choice_for_unitttest = int(menu_choice_for_unitttest)
121

122
        if menu_choice_for_unitttest == 1:
123
            print("Expected value for test to pass:")
124
            expected_value = int(input())
125
            run_the unit test.the unit test_for_assert_equals(result, expected_value)
126
        elif menu_choice_for_unitttest == 2:
127
            print("Expected value for test to pass:")
128
            expected_value = int(input())
129
            run_the unit test.the unit test_for_assert_true(result, expected_value)
130
        elif menu_choice_for_unitttest == 3:
131
            print("Expected value for test to pass:")
132
            expected_value = int(input())
133
            run_the unit test.the unit test_for_assert_false(result, expected_value)
134

135
        print(
136
            "To go on with the code getting executed, enter input [y] or [n]")
137
        continue_or_exit = input()
138

139
        if continue_or_exit == 'y' or continue_or_exit == 'Y':
140
            pass
141
        elif continue_or_exit == 'n' or continue_or_exit == 'N':
142
            sys.exit()
143

144

145
if __name__ == '__main__':
146
    main()
147

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.