Testing a Basic Script - Preparing the Test Bed

Python 3: Automating Your Job Tasks Superhero Level: Automate Unit Testing with Python 3
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€91.90
List Price:  €128.67
You save:  €36.76
£78.70
List Price:  £110.19
You save:  £31.48
CA$136.14
List Price:  CA$190.60
You save:  CA$54.46
A$149.21
List Price:  A$208.90
You save:  A$59.69
S$134.58
List Price:  S$188.42
You save:  S$53.84
HK$780.18
List Price:  HK$1,092.29
You save:  HK$312.10
CHF 90.89
List Price:  CHF 127.25
You save:  CHF 36.36
NOK kr1,073.95
List Price:  NOK kr1,503.58
You save:  NOK kr429.62
DKK kr686.39
List Price:  DKK kr960.97
You save:  DKK kr274.58
NZ$162.99
List Price:  NZ$228.20
You save:  NZ$65.20
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,707.96
List Price:  ৳16,391.62
You save:  ৳4,683.65
₹8,329.22
List Price:  ₹11,661.24
You save:  ₹3,332.02
RM468.70
List Price:  RM656.20
You save:  RM187.50
₦146,985.30
List Price:  ₦205,785.30
You save:  ₦58,800
₨27,706.17
List Price:  ₨38,789.74
You save:  ₨11,083.57
฿3,624.43
List Price:  ฿5,074.35
You save:  ฿1,449.91
₺3,224.01
List Price:  ₺4,513.75
You save:  ₺1,289.73
B$510.40
List Price:  B$714.59
You save:  B$204.18
R1,807.25
List Price:  R2,530.23
You save:  R722.97
Лв179.86
List Price:  Лв251.81
You save:  Лв71.95
₩135,425.45
List Price:  ₩189,601.05
You save:  ₩54,175.60
₪370.41
List Price:  ₪518.60
You save:  ₪148.18
₱5,770.97
List Price:  ₱8,079.59
You save:  ₱2,308.62
¥15,565.94
List Price:  ¥21,792.94
You save:  ¥6,227
MX$1,660.69
List Price:  MX$2,325.03
You save:  MX$664.34
QR362.75
List Price:  QR507.87
You save:  QR145.11
P1,354.73
List Price:  P1,896.68
You save:  P541.94
KSh13,098.69
List Price:  KSh18,338.69
You save:  KSh5,240
E£4,690.53
List Price:  E£6,566.93
You save:  E£1,876.40
ብር5,727.42
List Price:  ብር8,018.62
You save:  ብር2,291.20
Kz84,733.22
List Price:  Kz118,629.90
You save:  Kz33,896.68
CLP$89,440.82
List Price:  CLP$125,220.73
You save:  CLP$35,779.90
CN¥722.24
List Price:  CN¥1,011.17
You save:  CN¥288.92
RD$5,826.59
List Price:  RD$8,157.46
You save:  RD$2,330.87
DA13,436.03
List Price:  DA18,810.98
You save:  DA5,374.95
FJ$222.79
List Price:  FJ$311.92
You save:  FJ$89.12
Q776.81
List Price:  Q1,087.56
You save:  Q310.75
GY$20,917.88
List Price:  GY$29,285.87
You save:  GY$8,367.99
ISK kr13,827.61
List Price:  ISK kr19,359.21
You save:  ISK kr5,531.60
DH990.33
List Price:  DH1,386.51
You save:  DH396.17
L1,760
List Price:  L2,464.07
You save:  L704.07
ден5,654.52
List Price:  ден7,916.55
You save:  ден2,262.03
MOP$803.51
List Price:  MOP$1,124.95
You save:  MOP$321.43
N$1,819.78
List Price:  N$2,547.77
You save:  N$727.98
C$3,674.63
List Price:  C$5,144.63
You save:  C$1,470
रु13,329.38
List Price:  रु18,661.67
You save:  रु5,332.28
S/371.45
List Price:  S/520.05
You save:  S/148.59
K388.01
List Price:  K543.23
You save:  K155.22
SAR374.96
List Price:  SAR524.96
You save:  SAR150
ZK2,538.86
List Price:  ZK3,554.51
You save:  ZK1,015.64
L457.57
List Price:  L640.62
You save:  L183.04
Kč2,271.57
List Price:  Kč3,180.29
You save:  Kč908.72
Ft35,609.88
List Price:  Ft49,855.26
You save:  Ft14,245.38
SEK kr1,071.59
List Price:  SEK kr1,500.27
You save:  SEK kr428.67
ARS$88,666.13
List Price:  ARS$124,136.13
You save:  ARS$35,470
Bs690.83
List Price:  Bs967.19
You save:  Bs276.36
COP$380,678.73
List Price:  COP$532,965.46
You save:  COP$152,286.72
₡51,169.76
List Price:  ₡71,639.71
You save:  ₡20,469.95
L2,462.78
List Price:  L3,448
You save:  L985.21
₲749,130.70
List Price:  ₲1,048,812.96
You save:  ₲299,682.25
$U3,853.49
List Price:  $U5,395.05
You save:  $U1,541.55
zł392.01
List Price:  zł548.83
You save:  zł156.82
Already have an account? Log In

Transcript

For this new lecture, I have created yet another folder on my D drive called testing, which I will use to store the files that I need to test a basic application. The application is called calculator.pi. Let me open it with notepad plus plus. and it consists of a function called calculator. You can see it right here, which takes two parameters, Excel path and price index. The Excel path parameter is passed to the read Excel method from within the pandas module.

This is the path to a basic Excel file that I have created in the same folder. So this is the file right here. Let me open it. This Excel file contains five values on The Price column, nothing more. So going back to the code on the first line inside the function, we are loading the Excel file as a panda's data frame, as we previously did in the data analysis. This section of this course.

Next, the price index parameter is passed as an index to the data frame in order to extract a certain value from the column. Since we have only five entries on this column, then all the valid values that price index can take are 0123 and four right, then by applying the float function on the value extracted from the data frame, we get the value of x one of the two values that we need to calculate the final result. Next, the second value which is y is entered by the user at the prompt. We are basically asking the user to enter a value and then we are converting that value which is initially a string to a floating point number using the float function. Finally, we are dividing x by y. Then we are raising the results to the power of two and then rounding up the results to the nearest integer.

Pretty straightforward, I think. Next, we need to test various others aspects of our application. What if the user enters a negative floating point number as the prompt? Is that going to break our application? What if the Excel file isn't found? How will the application handle this scenario?

Finally, we need to test the calculator using all five values inside the Excel column, and check if the result is the correct one. Of course, these are examples of basic tests on a basic application. However, you should treat these videos as laying the foundation of your future testing skills. First, let's see the tests that I wrote for testing several types of user input. I have grouped these tests together inside the test underscore input.py file inside the class called test class. Of course, in order to test the calculator function from within the calculator.py file, I had to import that function into the namespace of this application.

Furthermore, I've also imported the mock object library from the unit test module. unit test is pythons own built in test automation module, and mock allows you to replace some parts of your system under test with mock objects. Now using mock and the patch method, we will simulate a user providing input at the prompt. Since this is an automated test treat, we need to eliminate the human interaction and somehow trick the application into believing that there is a real user entering values at the prompt for each test. That's why the first argument in between parentheses also called the target references the input function built in is a module providing direct access to all of pythons built in functions. For instance, built ins dot range is the full name of the built in arrange function, while built ins dot input is the full name of pythons input function.

The second argument is the value of That gets passed as input to the input function. So using these two lines of code right here, we are passing the value of 10 as input for our calculator, this will translate to the value of y. And then on the next line, we are asserting if the result returned by the calculator function having 10 as the y value and the entry at index zero in the data frame as the x value. So we are calculating if the final result returned by that function is equal to 894. Of course, I've calculated this value myself by simply dividing x, which is the value at index zero in the data frame, that would be 299 by y, which is, as I said, 10, then raising the resulting value to the power of two and finally rounding up to the nearest integer as part of the formula inside the main function.

Going back to test underscore input dot p y the remaining thing Free tests that you can see right here are performing similar operations, only that the value of y changes each time, because we want to test various types of user input, positive integer, negative integer, positive float, and finally negative float. Now it's time to run this test suite. First, let's go to the command line. And let's move to the correct folder, CD testing. And I'm going to enter pi TEST TEST underscore input.pi. So let's scroll up a bit.

And notice that we have four items collected. The fourth test inside the test underscore input.py file. All of these tests passed since I provided the correct values to test against inside all the assert statements. Also, going back to the command line, we see a lot of other output and various warning messages coming from some of the modules that work together to perform the testing process, I usually ignore these messages and focus on the test results. However, the question may be how to get rid of this ugly output and see only the relevant information when running the test suite. Well, there's a special option that we can add to our PI test command.

And this option will suppress any warning messages when running the tests. So let me show this to you by test, dash dash, disable dash warning, best underscore input dot p y, enter. Okay, much better. Now let's focus on the second test suite in our folder. That would be test exceptions dot p y Let me open this file. The three tests that you can see inside this file, also grouped in a class called test class will test our calculator application for any exceptions that might be raised in certain scenarios.

For instance, the first test inside the class this one right here checks whether wrong path to the Excel file will trigger the file not found error exception or not. So we are again simulating the user input using mock dot batch. And we are also checking if the exception is raised as a result of providing a wrong path as a parameter for the calculator function. Notice that in the first test, I am passing the testing value dot XLS x as a path instead of the valid path, which is actually the testing values dot XLS x. Okay, in the next test, we are evaluating if index seven passed as a parameter to the calculator function and then further passed to the data frame is a valid index for our data frame. And since we only have five elements in this column, index seven will certainly raise an exception, the key error exception and that's exactly what the second test is evaluating Finally, the third test right here checks whether the zero division error exception is triggered when the user enters zero at the prompt, notice the value of zero for the return underscore value argument.

This value is then mapped to the y value in the formula, which is the denominator when dividing x by y. Of course, this will raise the expected exception. And let's see this by running all three tests in the file. So we have pi test, dash dash disabled warning, test underscore exceptions that p y Enter, and all three tests best notice the three green dots right here, which means that the expected exceptions have been raised when the required conditions are cured. Great. Finally, the third test suite in the file, which is test underscore calculation that p y is actually checking if the calculations are done correctly for each of the five values.

Inside the data frame inside the Excel file, whilst keeping the same value for the user input, which is 10. I think that these tests are very straightforward and need no further explanation. However, let's run this test suite as well and see if the calculator works as expected. So, back to the command line, and I will replace the name of the file with test underscore calculation dot P, y, enter. Okay, great. All five tests passed so we can move on to the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.