You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#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.
#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
10
+
def__init__(self):
11
+
pass
12
+
13
+
14
+
# 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
15
+
defperform_addition(self):
16
+
operation_name="addition"
17
+
print("Enter the first number for {} operation".format(operation_name))
18
+
num_1=int(input())
19
+
print("Enter the second number for {} operation".format(operation_name))
20
+
num_2=int(input())
21
+
22
+
resultant=num_1+num_2
23
+
returnresultant
24
+
25
+
# 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
26
+
defperform_subtraction(self):
27
+
operation_name="subtraction"
28
+
print("Enter the first number for {} operation".format(operation_name))
29
+
num_1=int(input())
30
+
print("Enter the second number for {} operation".format(operation_name))
31
+
num_2=int(input())
32
+
33
+
resultant=num_1-num_2
34
+
returnresultant
35
+
# 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
36
+
37
+
defperform_multiplication(self):
38
+
operation_name="multiplication"
39
+
print("Enter the first number for {} operation".format(operation_name))
40
+
num_1=int(input())
41
+
print("Enter the second number for {} operation".format(operation_name))
42
+
num_2=int(input())
43
+
44
+
resultant=num_1+num_2
45
+
returnresultant
46
+
47
+
# 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
48
+
defperform_division(self):
49
+
operation_name="division"
50
+
print("Enter the first number for {} operation".format(operation_name))
51
+
num_1=int(input())
52
+
print("Enter the second number for {} operation".format(operation_name))
53
+
num_2=int(input())
54
+
55
+
resultant=num_1/num_2
56
+
returnresultant
57
+
58
+
# 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
# 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
# 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
# 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.
84
+
defmain():
85
+
86
+
run_theunittest=BasicMathOperationsTesting()
87
+
88
+
while(True):
89
+
90
+
# from the listed below the list of operations select any one of the operations
91
+
print("Select any of the mathematical operations which are listed below:")
92
+
print("1. To perform the addition operation and then perform the unit test on the result obtained.")
93
+
print("2. To perform the subtraction operation and then perform the unit test on the result obtained.")
94
+
print("3. To perform the multiplication operation and then perform the unit test on the result obtained.")
95
+
print("4. To perform the division operation and then perform the unit test on the result obtained.")
96
+
print("5. To exit from the code execution.")
97
+
98
+
menu_choice=input()
99
+
menu_choice=int(menu_choice)
100
+
101
+
ifmenu_choice==1:
102
+
result=run_theunittest.perform_addition()
103
+
elifmenu_choice==2:
104
+
result=run_theunittest.perform_subtraction()
105
+
elifmenu_choice==3:
106
+
result=run_theunittest.perform_multiplication()
107
+
elifmenu_choice==4:
108
+
result=run_theunittest.perform_division()
109
+
elifmenu_choice==5:
110
+
sys.exit()
111
+
112
+
print("Select any of the unit tests to perform which are listed below:")
113
+
print("1. To perform the assertEqual the unit test on the above done mathematical operation.")
114
+
print("2. To perform the assertTrue the unit test on the above done mathematical operation.")
115
+
print("3. To perform the assertFalse the unit test on the above done mathematical operation.")
This Script is an AUTOMATED Software testing before that we need to import unittest , sys Then just we need to run AUTOMATED_Software_Testing.py simple and easy to use.
7
+
8
+
9
+
**conclusion:**
10
+
11
+
This Just a Script That we can allow to AUTOMATE the Software testing from the terminal!
0 commit comments