Running Multiple Tests. Test Discovery Rules in Action

Python 3: Automating Your Job Tasks Superhero Level: Automate Unit Testing with Python 3
6 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
€92.19
List Price:  €129.06
You save:  €36.87
£78.58
List Price:  £110.01
You save:  £31.43
CA$136.67
List Price:  CA$191.35
You save:  CA$54.67
A$150.59
List Price:  A$210.84
You save:  A$60.24
S$134.92
List Price:  S$188.90
You save:  S$53.97
HK$780.74
List Price:  HK$1,093.06
You save:  HK$312.32
CHF 91.36
List Price:  CHF 127.90
You save:  CHF 36.54
NOK kr1,064.83
List Price:  NOK kr1,490.80
You save:  NOK kr425.97
DKK kr687.94
List Price:  DKK kr963.14
You save:  DKK kr275.20
NZ$163.14
List Price:  NZ$228.40
You save:  NZ$65.26
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,726.90
List Price:  ৳16,418.13
You save:  ৳4,691.23
₹8,323.81
List Price:  ₹11,653.68
You save:  ₹3,329.86
RM470.40
List Price:  RM658.58
You save:  RM188.18
₦144,735.52
List Price:  ₦202,635.52
You save:  ₦57,900
₨27,863.65
List Price:  ₨39,010.23
You save:  ₨11,146.57
฿3,652.73
List Price:  ฿5,113.97
You save:  ฿1,461.24
₺3,221.22
List Price:  ₺4,509.83
You save:  ₺1,288.61
B$515.31
List Price:  B$721.46
You save:  B$206.14
R1,838.13
List Price:  R2,573.45
You save:  R735.32
Лв180.33
List Price:  Лв252.47
You save:  Лв72.14
₩136,328.34
List Price:  ₩190,865.12
You save:  ₩54,536.78
₪367.16
List Price:  ₪514.04
You save:  ₪146.88
₱5,817.18
List Price:  ₱8,144.28
You save:  ₱2,327.10
¥15,665.20
List Price:  ¥21,931.91
You save:  ¥6,266.71
MX$1,664.50
List Price:  MX$2,330.37
You save:  MX$665.86
QR364.97
List Price:  QR510.97
You save:  QR146
P1,352.70
List Price:  P1,893.83
You save:  P541.13
KSh13,248.67
List Price:  KSh18,548.67
You save:  KSh5,300
E£4,709.52
List Price:  E£6,593.52
You save:  E£1,884
ብር5,750.98
List Price:  ብር8,051.60
You save:  ብር2,300.62
Kz84,938.73
List Price:  Kz118,917.63
You save:  Kz33,978.89
CLP$89,860.01
List Price:  CLP$125,807.61
You save:  CLP$35,947.60
CN¥710.98
List Price:  CN¥995.41
You save:  CN¥284.42
RD$5,874.34
List Price:  RD$8,224.32
You save:  RD$2,349.97
DA13,453.05
List Price:  DA18,834.81
You save:  DA5,381.76
FJ$226.58
List Price:  FJ$317.23
You save:  FJ$90.64
Q777.82
List Price:  Q1,088.99
You save:  Q311.16
GY$20,943.47
List Price:  GY$29,321.70
You save:  GY$8,378.22
ISK kr13,838.61
List Price:  ISK kr19,374.61
You save:  ISK kr5,536
DH991.17
List Price:  DH1,387.67
You save:  DH396.50
L1,767.86
List Price:  L2,475.08
You save:  L707.21
ден5,684.36
List Price:  ден7,958.33
You save:  ден2,273.97
MOP$804.86
List Price:  MOP$1,126.84
You save:  MOP$321.97
N$1,819.29
List Price:  N$2,547.09
You save:  N$727.79
C$3,684.41
List Price:  C$5,158.32
You save:  C$1,473.91
रु13,334.80
List Price:  रु18,669.25
You save:  रु5,334.45
S/373.79
List Price:  S/523.33
You save:  S/149.53
K389.03
List Price:  K544.66
You save:  K155.63
SAR375.02
List Price:  SAR525.05
You save:  SAR150.02
ZK2,610.17
List Price:  ZK3,654.34
You save:  ZK1,044.17
L458.69
List Price:  L642.19
You save:  L183.49
Kč2,277.99
List Price:  Kč3,189.28
You save:  Kč911.28
Ft35,684.59
List Price:  Ft49,959.85
You save:  Ft14,275.26
SEK kr1,068.91
List Price:  SEK kr1,496.52
You save:  SEK kr427.60
ARS$88,989.05
List Price:  ARS$124,588.23
You save:  ARS$35,599.18
Bs691.73
List Price:  Bs968.45
You save:  Bs276.72
COP$381,035.35
List Price:  COP$533,464.74
You save:  COP$152,429.38
₡51,327.13
List Price:  ₡71,860.04
You save:  ₡20,532.90
L2,473.92
List Price:  L3,463.59
You save:  L989.66
₲753,154.66
List Price:  ₲1,054,446.66
You save:  ₲301,291.99
$U3,830.21
List Price:  $U5,362.45
You save:  $U1,532.23
zł393.43
List Price:  zł550.82
You save:  zł157.39
Already have an account? Log In

Transcript

Following up on the previous lecture, let's see how to run multiple tests in a test suite and see more examples of how the test discovery mechanism works. For this lecture, I have created a new folder on my D drive called basic. And inside this folder, I have three Python files. Let's discuss each of them one by one. As a side note, you can download these files from the notebook following this video. First, let's see the basic.py file.

So I'm going to open up this file in Notepad plus plus, you already know most of the code in this file from the previous video. We have the same myfunc function right here, taking three parameters and returning the result of adding the values of x, y, and z. Next, we have the same my exception function, which returns the result of dividing by zero. And down below you can find the same function which evaluate If my exception function raises the zero division error exception or not, nothing new thus far. The new thing here is the test class class. Remember that you can group multiple tests together inside the same class.

What you should keep in mind here is that according to the test discovery roles, you need to prefix your class name using test. So that's why I chose this name. test with a capital T. Because class names should be written in camel case, as you might remember from earlier in the course, by test will identify all the classes whose names start with test, and we'll run all the test functions inside those classes. As long as each function name starts with test underscore, my class which I called test class is a parent class and doesn't inherit anything from other classes. That's why we should insert objects in between it's parenthesis by default. As for the text form, Inside the class, always remember to insert self as the first parameter of each method.

The functions outside the class do not need the self parameter. However, the ones inside the class should always take this parameter. Besides these differences, the two functions inside the class are evaluating the result of passing One, two and three as arguments to the myfunc function, and asserting if the result is five and six respectively. Obviously, the first test will fail, and the second one will pass. Also, notice that for each assert statement, you can also insert a comment after the comma and in between double quotes, which can be useful when having a large number of tests or a higher degree of complexity in your testing. Now let's head over to the command line and run our basic.py file.

So first, let's go to the directory in which the file is saved. So CD Basic. And now we have pi test, dash v basic dot P, y, enter. So the first thing you notice is that pi test has collected three items. These are actually the three tests inside our basic.py file. This means that all the tests in the file have been properly discovered.

Next, we see that one test has failed the first one, and the other two tests passed. Of course, that was expected to happen this way. Since test underscore result one evaluates six equals equals five, which is obviously false, was the other two tests ran without any issues, and the assertions made were true. Finally, going back to the command line, notice the comment that we entered as a string after the comma being printed out to the screen when the assertion error exception gets raised. Great. Now let's move on to the other two files in the folder.

So Let me open both of them using notepad plus plus. So first of all, we have result for underscore test.py, which imports the myfunc function from the basic.py file, which is located in the same directory, and then define a function called test underscore result for that simply performs the same evaluation we've already seen. Next, the other file is called test underscore result five dot p y, which again, imports the myfunc function and evaluates my funk of one, two and three equals equals five, nothing new inside these two files. So why am I showing you code that you've already seen? Well, that's because I simply want to show you the test discovery mechanism in action once again. Therefore, if you go back to the command line, first make sure you are located in the correct folder.

And then you can simply issue the PI test command to automatically discover and run the tests in this folder. Follow the test discovery rules. So I'm just going to enter pi test enter. At this point, we can draw the following conclusions. First, the basic.py file located in the same folder is ignored, because it doesn't follow the rules of test discovery. More precisely, its name does not start with test underscore, or it doesn't end with underscore test.

That's why you should specify the name of the file basic dot p y when running the PI test command in order for the test inside this file to be discovered and executed. On the other hand, the only files that meet the naming condition are result underscore four dot p y and test underscore result five dot p y. So these two items get collected, and the result of running them is the one on the screen, one passed and one failed as expected. Having that said I hope you now understand the way in which to run multiple tests at once and also So how to properly name your files, classes and functions when designing your test suites. I'll see you in the next video.

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.