Installing pytest and Writing Your First Test

Python 3: Automating Your Job Tasks Superhero Level: Automate Unit Testing with Python 3
12 minutes
Share the link to this page
You need to have access to the item to view this lesson.
One-time Fee
List Price:  $139.99
You save:  $40
List Price:  €128.51
You save:  €36.71
List Price:  £110.53
You save:  £31.58
List Price:  CA$190.63
You save:  CA$54.47
List Price:  A$208.90
You save:  A$59.69
List Price:  S$188.42
You save:  S$53.84
List Price:  HK$1,092.29
You save:  HK$312.10
CHF 91.18
List Price:  CHF 127.65
You save:  CHF 36.47
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
List Price:  NZ$228.20
You save:  NZ$65.20
List Price:  د.إ514.18
You save:  د.إ146.92
List Price:  ৳16,307.21
You save:  ৳4,659.53
List Price:  ₹11,661.24
You save:  ₹3,332.02
List Price:  RM656.20
You save:  RM187.50
List Price:  ₦205,785.30
You save:  ₦58,800
List Price:  ₨38,789.74
You save:  ₨11,083.57
List Price:  ฿5,038.70
You save:  ฿1,439.73
List Price:  ₺4,513.75
You save:  ₺1,289.73
List Price:  B$714.59
You save:  B$204.18
List Price:  R2,565.91
You save:  R733.17
List Price:  Лв251.83
You save:  Лв71.95
List Price:  ₩189,601.05
You save:  ₩54,175.60
List Price:  ₪516.98
You save:  ₪147.71
List Price:  ₱8,098.42
You save:  ₱2,313.99
List Price:  ¥21,792.94
You save:  ¥6,227
List Price:  MX$2,325.03
You save:  MX$664.34
List Price:  QR507.87
You save:  QR145.11
List Price:  P1,886.91
You save:  P539.15
List Price:  KSh18,172.80
You save:  KSh5,192.60
List Price:  E£6,553.98
You save:  E£1,872.69
List Price:  ብር7,998.81
You save:  ብር2,285.53
List Price:  Kz118,851.51
You save:  Kz33,960
List Price:  CLP$125,220.73
You save:  CLP$35,779.90
List Price:  CN¥1,011.17
You save:  CN¥288.92
List Price:  RD$8,157.46
You save:  RD$2,330.87
List Price:  DA18,848.39
You save:  DA5,385.64
List Price:  FJ$311.92
You save:  FJ$89.12
List Price:  Q1,081.96
You save:  Q309.15
List Price:  GY$29,135.07
You save:  GY$8,324.90
ISK kr13,781.37
List Price:  ISK kr19,294.47
You save:  ISK kr5,513.10
List Price:  DH1,386.51
You save:  DH396.17
List Price:  L2,464.07
You save:  L704.07
List Price:  ден7,916.55
You save:  ден2,262.03
List Price:  MOP$1,119.16
You save:  MOP$319.78
List Price:  N$2,547.77
You save:  N$727.98
List Price:  C$5,126.52
You save:  C$1,464.82
List Price:  रु18,565.58
You save:  रु5,304.83
List Price:  S/520.05
You save:  S/148.59
List Price:  K540.79
You save:  K154.52
List Price:  SAR524.96
You save:  SAR150
List Price:  ZK3,554.51
You save:  ZK1,015.64
List Price:  L640.62
You save:  L183.04
List Price:  Kč3,180.29
You save:  Kč908.72
List Price:  Ft49,813.32
You save:  Ft14,233.39
SEK kr1,071.59
List Price:  SEK kr1,500.27
You save:  SEK kr428.67
List Price:  ARS$123,415.66
You save:  ARS$35,264.13
List Price:  Bs962.21
You save:  Bs274.93
List Price:  COP$532,965.46
You save:  COP$152,286.72
List Price:  ₡71,270.84
You save:  ₡20,364.55
List Price:  L3,448
You save:  L985.21
List Price:  ₲1,043,416.42
You save:  ₲298,140.27
List Price:  $U5,431.06
You save:  $U1,551.84
List Price:  zł548.49
You save:  zł156.72
Already have an account? Log In


Hi, and welcome to this section on test automation. During this section of the course, you're going to learn about and use the PI test module, one of the most popular Python libraries dedicated to unit and functional testing and test automation. So without further ado, let's get to work. Now, first of all, you should install pi test on your own system by simply going to the windows command line or the terminal if you are on a Mac iOS. And in my case for Windows, I will type in beep install by test, wait for the download and installation process to finish and then you're good to go. You can pause the video right now and installed pi test on your own system.

I'm not going to do that once again since I've already installed it. Now assuming you pause the video and you now have pi test installed, let's check the version of pi test by running pi test. dash dash version and enter. I currently have pi test version four dot four dot zero on my computer, this being the latest stable version at the date of this recording. However, in your case, this version might differ depending on when you are watching this video. Also, whenever you need to check all the available features and command options of pi test, you can simply run by test, dash dash help in the command line to get more information about the capabilities of this module.

Finally, pi test has a comprehensive documentation, and I definitely recommend checking it out whenever you need to understand more about this library and its features. The documentation is available at Doc's dot pi slash n slash latest slash contents dot html. bookmark this link and now let's move on to writing your first test. So I have created a folder on my D drive called for Inside this folder, let's create a new Python script called first dot p y, I have already created this script. And I'm going to have this file open in Notepad plus plus, by the way, you can find this file attached to the notebook following this video. The first thing to write is the code to be tested.

And let's choose a simple function that takes three parameters and adds them together. So let me define the function def, my underscore func, of let's say x, y and z, and we have return x plus y plus z. Okay, so this is the function that we are going to test. Next let's also write the test itself as a new function. Let me write this first and then we will discuss it. So let's say def test, underscore result one, open and close parenthesis colon, and then assert myfunc of let's say one Two and three equals equals five.

Okay, first of all, please notice the name of this function test underscore result one, by test has a set of rules for discovering tests inside the folder. When starting the test suite, you can find these rules right here. I'm going to attach this link to the video. So these are the rules conventions for Python test discovery. Let me summarize this information. So when running the PI test command in the windows command line, don't worry, we will do that very soon.

By test looks in the current directory, and in the sub directories of the current directory, that is if the user hasn't configured certain default directories to look into. Let's assume that pi test looks in the current directory and all of its sub directories. So in the command line if we position ourselves in the folder I just created, so the and now CD First, and then pi test will search for any test inside this folder and all of its subfolders, if any. But how does pi test identify the tests in this location? Well, it looks for any Python scripts whose names start with test underscore, or end with underscore test. Next inside each of these Python files matching this criteria by test searches for function names starting with test underscore outside of a class, and function names starting with test underscore inside of classes, only classes whose names start with test with a capital T. You'll see an example of a class in the next lecture.

For now let's get back to our code in Notepad plus plus, inside the test function. We are using the assert statement right here to evaluate if the result of passing these three arguments to the myfunc function is indeed five. Please notice the double equal sign Right here that is used to evaluate the equality of two expressions. Of course, by quickly looking at the function above, we realized that adding one, two and three would return a final result of six and not five. So this means that our test should fail. Make sure your file is saved, so Ctrl S, and now let's head over to the command line to run our test.

So at this point, I am positioned in the folder called first. And now let me type in PI test, enter. Okay, it seems that at this point pi test hasn't been able to identify any tests in this location in the folder called first. Why? Well, let's return to the test discovery rules in the current directory by test hasn't found any Python files whose names start with test underscore or end with underscore test. That's why it thinks that there are no tests to run inside this folder.

By the way, you can ignore this new folder being created right here. This is by test on cache, which is created at runtime whenever you run a test. However, going back to our scenario, we do have a test inside our file, right. In order to run that test, we need to explicitly specify the name of the file, holding the test when running the PI test command. So let's get back to the windows command line. And I'm going to use by test and now the name of the file first dot p y, enter.

Okay, at this time, it seems that we have some results. Now let's analyze this output a bit. First, notice that we get the platform right here platform win 32, and also the versions of Python and pi test currently running on this system. So in my case, the platform is as I said, win 32 because I'm using a Windows 10 operating system. And I also have Python version three dot seven dot two and Python. Four dot four dot zero.

On the next row right here, notice that we have the so called root, Dir or root directory displayed. This is the folder from which our test has been executed. In my case, that is the folder called first located on my D drive. Next, you can see this line right here, collected one item. This means that pi test has identified a single test inside the file. Based on the test discovery rules we have discussed earlier in this video, meaning it has found the test underscore result one function inside the file and ran it.

Now let's get back to the output. You can notice this red f right here, this signals that the test has failed. And now we can move over to the failures section. So this section right here to find out more about this failed test. Notice that pi test has identified the exact location of the code causing the failure And that is the assert statement, also pointing out that myfunc of one, two and three, returns six and not five. That's why the assertion error exception has been raised and the test failed in zero point 11 seconds.

Okay, great job running your first test, although it is a failed test. Another type of test that we can run, it's one that checks if a certain exception is raised by a piece of code a function. Let me write such a test and then we will discuss it as well. I'm going to go back to notepad plus plus. And I'm going to start by writing the function that is going to be tested. So I'm going to delete this code right here and define a new function def, of let's say my exception, open and close parentheses.

Let's say div equals 10 divided by zero, and we want to return a div. So this function does nothing more than to return the result of the Dividing 10 by zero, of course, this operation is not permitted, and it will definitely raise the zero division error exception. Am I right? Now let's write the test function as well. So def of test underscore result one. And let's say with PI test dot raises, opening close parentheses.

And now we should specify the exception zero division error, colon, and my exception open and close parentheses. Again, we have to name the function in such a way that pi test will be able to identify it as a test. So that's the result one is the name of my function. Next to evaluate if the my exception function raises the zero division error exception or not, we can use the width statement and the raises method from within the PI test module. Underneath the width statement, indented one level to the right of course. We just need to input the name of the function to be evaluated.

Also, since we are calling a method from the PI test module, we need to import that module into the namespace of our script. So let me add import by test. Okay, and that's it. Now make sure your file is saved. And let's run it once again in the windows command line. So pi test first dot P, y, and enter.

Okay, notice that this time we have a green dot right here, instead of a red F, meaning that the test has passed. Of course, looking at our code, we expected this test to pass since the my exception function raises the zero division error exception, and the test result one function evaluates that as being true. The last things I'll show you in this video are some additional options that you can use in the command line to modify the output of your tests. For instance, using the dash v option will increase the verbosity, meaning the level of detail in your output compared to the output we've just seen. So let's try this by test, dash V, first dot v y, enter. This time, notice that the name of the test is now shown test result one.

And we also notice the word past instead of the green.on. The other hand for a quieter output, meaning a low verbosity level, you can use the dash q option. So let's try that as well, by test dash Q, first dot p y. This time, we only see the green dots signaling past tests and the time it took to run the test, which is 0.01 seconds. Furthermore, you can use the dash r option to display a short summary of the tests that have been performed in combination with the F option for showing only failed tests or the P option for showing only the past tests. So in our case, Let's try this by test dash r b first dot p y.

Okay, this time you can notice the short test summary info section, which highlights the past test by displaying their name and marking them as passed. For all the other available options, just use PI test, dash dash help whenever you need to customize your output. That's it for now. I'll see you in the next lecture.

Sign Up


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.