Learn Pytest in 60 Minutes : Python Unit Testing Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to this video on Python unit testing tutorial for beginners using PI tests so first of all what is unit testing so according to Wikipedia unit testing is a software testing method by which individual units of source code are tested to determine whether they are fit to use or not so in unit testing we perform the lowest level of testing that is individual units of software are tested that means units like functions subroutines and classes are tested to verify if they are giving the desired result or not now let's discuss why should be unit test our code so when you write unit tests they reduces bug in the new features and existing features so let's say if you are writing a new feature in your code unit tests can help you to reduce the bugs which may appears during the execution of your code now if you write good unit tests this serves as a good documentation for the person who is using your code so because in unit test we are actually using the functions or the classes and testing them so if the person who wants to use your code he reads the test he will be able to understand how to use your code also when you write tests for your code it reduces the cost of the change in the production so if some code goes to production without testing and you find the bug in the production stage then it's much costlier than fixing your code when you are developing your code next unit test helps in faster debugging and faster development of your program also because you have the idea of what should be the desired result that means you can develop your code in Manor which is perfect for your tests and at last but not the least unit test helps us to better design our program now let's talk about the unit testing frameworks which are available in Python so I have listed here three top most unit testing frameworks which we can use to write our unit tests so first module is unit test module and this unit tests module is present in your Python standard library so you don't need to install any extra module in order to use this unit test module the next module is the nose module which is not there in the standard library but it's simpler to use than the unit test framework and at last you can use the PI test framework which is not in the Python standard library but it's one of the most popular unit testing framework for Python so we are going to use PI test for the unit testing of our python code so let's see how we can install PI test package on our operating system on which we are working on so you can install a PI test using pip so we are going to just write pip install PI test and then press Enter which is going to install this PI test package on our operating system so you can see PI test is now installed on my operating system so I'm going to give CLS command to clear a terminal and then first of all I can give this PI test command and then - H in order to get the help about PI test so just press Enter which is going to give you all the flags or the commands which you can use with the PI test so the usage is given here you just need to use the PI test keyword and then you can give the options which are listed below and you can give the file or directory' name or the list of files or directories so for example the general options are given here so when you give this flag - K or - K it's going to match the substring or the regular expression which you provide after this flag and when you use - M this means that it's going to test the functions which have the name assigned directly to them that means only run the tests which have a given mark expression so I'm going to try to show you all these options one by one but let's see how we can write the test to use them using PI test so in here I have a Python script called math underscore func dot py file and inside this script I have two functions one is a simple add function which adds two values and other is the product function which returns the value of the multiplication of two values now in order to write the unit test for these functions I can create a new file so let me create a new Python file and I'm going to name it as test underscore math underscore func so I have added this extra prefix here which is test underscore in front of the name of my file which is math func so I'm going to just create this file so this test underscore prefix which I have added in front of my Python script name will help pi test to recognize that this is the file in which the unit testing functions are written so it's going to go to that file and here we can first of all import the math func Python script and then we can define the test functions so the test function you can define simply as you define any other function in your Python script so you can write def and then just start the test name with the test prefix so test underscore and then whatever name you want to give to your test so first of all I want to test the add function here so I'm going to just write test underscore add and this function is not going to take any parameter and inside this function I can provide some assertion for my functions so I can use a special keyword which is assert and then you can call your function in your Python script so Matt funk thought add function and here as an argument I'm going to provide two numbers let's say seven and three which is going to give us the result then so we are going to just check whether this function is returning 10 or not and this is called assertion assertion means you want to assert that the function is returning the desired result so we are expecting the result 10 when we provide 7 and 3 as an argument to this add function you can also provide multiple asserts here so let's say I just want to provide for example 7 here you can see that Y is equal to 2 is the default value of y so here in the assert I can only give one argument and the value of second argument which is Y will be taken from the default value which is 2 so 7 plus 2 is 9 so we can just say whenever you provide 7 here it's going to give us 9 once again whenever you provide 5 here it's going to give us 7 so in this way you can provide multiple asserts inside your function whatever you think will come as an expected result of your function so I'm going to define the second function which is test product and once again I'm going to pass no argument here and once again I can just use this assert keyword and then the name of my library which is math func dot the function this time we are going to test the product function and let's say we just want to check the product of 5 and 5 and the expected result will be 25 in a same way whenever you provide here for example only one argument the default value of y is 2 right so when we provide only one argument which is 5 here we will be expecting 10 here in a similar way when I provide let's say 7 here are expecting the result 14 here so once we have written our unit tests let's try to run these tests using PI test so first of all I need to go to the directory where this test script is there so I'm going to just copy the path and then go to the terminal and I'm going to CD to this path which I have copied and let me just remove the script name from here and then press Enter so now I'm in the folder where my matte func dot py is there and test underscore matte underscore func dot py file is there so in order to run your unit test file you just need to give this command PI test and the name of your test file which is test underscore math underscore func dot py in my case and then I'm going to press enter and you can see it's going to give me two passed in whatever time it took to execute these two tests so because we have written two tests here for two functions it's going to show us this result which is two passed let's say we are going to expect some unexpected values so here we have written some wrong value which we are expecting and now when we run our test is going to fail you can see one passed and one failed and PI tests will also give you the information about the assertion failure so the assertion failure is happened at this assertion where you are just testing 10 is equal to 18 which is wrong here so once again we are going to correct this all so let's say some developer has changed the function which you are testing so instead of returning the Edition now I'm going to return the subtraction which is going to break that test now so I'm going to run the test once again and once again it's going to give you the error and this time it's going to show us that the result is 4 but we are expecting 10 as the result and that's why it's failing so once again I'm going to just fix my code and then once again I'm going to run my test and once again the test will pass so let me just clear the terminal using CLS command and you can also give a flag called - V which stands for verbose in order to get the more detailed result so I'm going to press ENTER and you can see it's going to give us this flag passed or failed in front of your tests and both tests are right now passing that's why it's written passed in green here let's break the test once again so I'm going to just say minus here and once again run the code and now it will say this test is passing but that test ad is failing due to this assertion so I'm going to once again fix the code and come to the terminal once again I'm going to just clear the terminal and you can also run your test using this command so you just need to write PI dot test without giving any name in front of this PI dot test and then press ENTER which is going to automatically recognize the test file because we have given the special prefix here test underscore this is how your PI test will recognize that this is the testing file so it's going to just run the test inside that file and once again you can see it runs and two tests are passing you can also give the verbose flag here which is going to give you the more information about your tests now let's say we are going to remove this prefix from our tests so let's say we just write tes underscore add so there is no test prefix in front of your function and once again I'm going to give the same command and you will see only one test is recognized which is test underscore product and this is because we haven't provided that test prefix in front of the function so in order to recognize this function as a unit test you need to give the test keyword as the prefix of your unit test function also whenever you change the name of your test file so let's say I'm going to refactor the name and I'm going to just remove the T from test and then once again I'm going to press refactor which is going to change the name of this test file and now this test file name is tes underscore Matt underscore func and once again I'm going to try to run my test and it's going to say no test run in this much amount of time so again whenever you don't give the test keyword in front of your file name then PI tests will not be able to recognize that this is your unit testing file let's go to the terminal and I'm going to just give the CLS command in order to clear the terminal and let's give the second command now which we have used for running the test which is the PI test command and the name of the file so now my file name is tes underscore math underscore py and now I'm going to press enter and you will see now it's going to recognize because we are specifically giving the filename in which our tests are there so if you want to run your test inside some file which doesn't have the test prefix in front of it you can just use PI test command and then give the name of the file and PI test is going to recognize that this is the file in which your unit tests are there and it will run all the unit tests in this file so in the last video we have written two test functions for our ad and product functions which were able to test those functions using these numbers now additionally I have added two more test functions here and these two test function I have added to test the strings with the add function and the product function so using this plus operator we can also add two strings right so I have added the string test for the add function and the product here now the first thing I want to show here is you can add any comparison operator here so it's not necessary that you need to use this equal to operator here you can also write for example greater than operator or greater than equals to operator or the less than operator or less than equals to operator or not equals to operator so you can use all these comparison operator with your assert statement additionally you can use this is keyword to test whether something is something so using this is keyword I am testing here that the result which I got using this ad function over the strings is it a string or not so using this type I'm just finding out the type of the result and I'm checking if this result is a string or not you can also use in operator or not in operator so using this not in keyword we are checking whether this string is inside our result or not so this string or let's say this string is not inside our result so this assert is going to pass because this string is not present in our result additionally you can also use this in keyword and you can check whether some string is inside your result or not so i can also write hello in result which will check if this keyword is inside our result or not now the next thing which i want to show here is let's say you just want to run one test from here which is test ad and you don't want to run any other tests how you can do it using command line so you can just write by test command and the name of your test file which is test underscore math underscore func in my case and then you can add double colon here and then gave the name of your test so let's say I just want to run that test ad so I'm going to just run this test and you can see it's going to only run the first test let me just add - we here which is going to show which test has passed and you can see this test has been passed which is test ad and let me just clear the terminal now the next option which I want to show here is - key so once again I'm going to just write PI test here and we will once again add the - V for Bobo's and you can use a special option which is - k and then you can give any expression which you want to match using this - k option so let's say we only want to run the ad test so we have to add test one is test underscore ad and other is test underscore ad underscore string so I just want to run the test for the add a function so here inside the double quotes I can just write ad and then press ENTER and it's going to run the test which contains add keyword or add string so you can see it ran test underscore ad and test underscore ad underscore string you can also use or operator here so let's say you want to run the test which contains ad or which contains string keyword and when I'm going to run the test it's going to run all those tests which contains either add or the string keywords so there are three tests which fulfills this condition and all those three tests are performed and other tests will be deselected so you can see for item and one item is d selected here also you can use the and instead of or so I'm going to just write end and then press ENTER and it's going to run only those tests which contains ad and string both so there is only one test name which contains ad and string both which is this one so only this will be suited and the other tree test will be deselected let me once again clear the terminal now the next option which I want to show here is the - Emma option so once again I'm going to just right here - M and after this - M you can provide any mark expression so let me go to my code and here I just need to import the PI test first of all so let me import PI test and here you will see that it's showing me the squiggly line because of my IDE which is PI charm so because I have installed a PI test using pip on the command line which is not recognized by my PyCharm IDE that's why it's showing me the squiggly line but I am running my test using command line so this will work so don't worry about the squiggly line the import will work and after that what I can do here is I can add a decorator so I can just write PI test dot mark dot any expression which you want to give here so this test add and test product is for the numbers so we can write number here and also we can add the same decorator for the test product and for the other two tests we can just write that these tests are for the strings so this expression is upon you so you can give any expression here which you want to match later using - M option so I'm going to also add this decorator over the other string function also so we have two markers now one is number and other is the string so now after - M you can give the mark expression name so let's say I'm going to just write number here and then press ENTER and it's going to run all the tests which have the mark number which is the test add and test product now let's run the test which have the mark string so I'm going to just write string and then press and it's showing me four be selected because I forgot this s after the string so let me just add this s and then press I can enter again and you can see it runs only those tests which have string mark on top of them so this is the string and this is the string marks so those two tests were executed and other two tests were these selected let me just clear the terminal once again now the next option which I want to show here is - X option so once again I'm going to just write PI test - V and then - X now this - X means exists first so whenever first failure occurs in your test the PI test will exit from the execution of your test so let's just add some failure assertion here so I'm going to add the failure here so let's say I'm just checking the failed assertion here just to check whether this - X works or not so I'm going to just press ENTER now and you can see first test was run which was passed but the other test failed so it has shown us the stack trace of the failed test and after that the text execution was stopped so after two tests because the failure occurred the other two tests which was supposed to be executed are not executed because of this - X option so as soon as the first failure occurs your test will exit now let's say you don't want to see this tech trace after the failure of your test so this is all the information which is given to you about what assertion has failed or not so let's say we don't want to see this so how to just skip that stack trace you can add - - TB is equal to no and this is going to disable the stack trace and and press enter and you can see now this text trace is not printed only the success or the failure will be printed but not the stack trace let me clear the terminal once again and this time I want to show one more option which is max peel so here you can just write - - max fail option and then gave the number in front of it after the equals - symbol so let's say we just write max fail - here then it's going to wait for the maximum number of failure and then it will exit so let me just run this test so you can see all four tests has been executed because maximum failure we were expecting is two that's why all four were executed because there was only one failure let's say I just make it one so now the value of max fail is 1 and then I press ENTER and you will see after the first failure it's going to exit because we have provided max fail 1 and this is the number of failures which we are expecting after that your test will be exited so let me just clear the terminal once again and let me show you one more option so this time I want to show you the Skip option so let's say I just remove all these markers from here and and now I want to add a mark which is skip so just add this decorator on top of your test which is at the rate test dot mark dot skip and then inside the parenthesis you can give the reason for the skip so here you can just write reason equals and whatever reason you want to give here so I'm going to just write do not run number add test ok so this is the reason which will be printed and when I will just execute my test once again with - V you can see this first test is kept so it's not executed it skipped and all the other three tests were executed let me just fix this second test so it's going to pass and once again let me just run this test and you can see all other tests were passed and the first test is skipped because we have added this skip decorator on top of the test add function also there is a skip if option available so here instead of skip you can just write skip if so this skip if is going to check for some condition so let's say I'm going to import the sis module here which is going to give me the information about the Python version I'm running and then as a first argument I can first of all give some condition so I'm going to just write sis dot version info which is going to give me the version of Python available and I'm going to check if this version is less than let's say 3.3 then I want to skip this test so I'm going to once again let me clear the terminal and once again run the test and I see some failure here which is here I added dot instead of comma so you need to separate this major and minor version using comma so this means 3.3 python version and once again I'm going to run the test and then press ENTER and now you can see all four tests were executed because this condition was not met now let's say I want to just give the opposite condition of that which means I want to skip if I have the Python version greater than 3.3 which I have I have the python version 3.7 let's check it using python command so i'm going to just write Python - - version and then press enter and you can see I have 3.7.0 version and let's run the test once again and now you can see this test is skipped because this condition is met now the next option I want to show here is - as options so let me just make this condition like this so this test will not be skipped and the next option we are going to see is the hyphen s option so let me clear the terminal and let's see which option I am talking about which is - s and this option you can use to print any output which you want to print using let's say print function so let's say I want to add a print statement here so I'm going to just add print and then let's say I want to just print the addition of seven and three and four just knowing whether this print statement has been executed I'm going to add this string here so we know that this print statement has been executed so now when I execute my PI test without the hyphen s option first you will see there is no print statement which is executed now if I just add - s here which will allow this print statement to be executed and now you will see that this print statement has been executed and this line is printed using this print statement so if you want to print all the print statements then use hyphen s option or you can also use the option which is - - capture is equal to no and then press ENTER which is also going to do the same so either you can use hyphen s or you can use - - capture is equal to no so let me clear this terminal one last time and the last option which I want to show here is the - queue option which is the quiet mode so let me just execute this PI test once again and you can see all this information is printed and when I add - queue here which is the quiet mode you can see no information is printed here about the test which test is passed only the important information is printed which is for test passed in this much amount of seconds if I remove the - V here from here you will see all the other output is gone and only for passed in whatever amount of seconds is printed so this is the quiet mode which will not print unnecessary information it's going to just show you how many tests passed and how many tests failed so this is how you can use some options with PI test command in this video I'm going to show you how to use a special decorator which is parameterize so here I have on the left hand side a simple function which is add and on the right hand side I have written that test for this function and I have written three test function for the add function the first test function here tests the add function using the integer values the second test function I have written for the string values and the third test function I have written for the float values which I provided to the add function now one thing which you will notice here is I'm using the same add function three times here in all these three tests and there has to be a better way in which we just need to call this add function only once now you can say that why we need three functions I can just write three assert into the same function and I can just remove these two other test functions this is okay but still I have to call this odd function three times to test it with numbers strings and the float values and in those type of situations you can use a special decorator which is parameterize in which you need to call a same function to test it with the different type of values so here I have imported this pi a test more you'll and then above my test function I will just call PI test first of all then dot mark dot parameterize so we are testing this ad function which takes two arguments so here in the parameter rise we just need to give the name of these two arguments you can just say that these two arguments will be X comma Y because I have given X comma Y here so I can just write into the single quotes X and then single quotes Y which will be the name of these two arguments so this name depends upon you you can say this is num1 and the y is let's say num2 or you can say this is r1 or r2 so these arguments names are user defined names you can give any names which you want to pass to your function so the number one variable name will be passed as the first argument and the number two variable name will be passed as the second argument now once again when you look closely to this assert you will see the variable values here are first argument second argument and the result so the same three values you need to give here so number one number two and the results so I'm going to just say that the third argument is result here and then as the fourth argument you give the iterate able list so the fourth argument will be a tray table list where you can provide the values of argument 1 argument 2 and the result so inside this list we are going to give this argument 1 2 and the result values using a tuple so here in the first test I have given 7 and 3 and the result is 10 we can give the same values here so 7 comma 3 and the result we are expecting is 10 so the 7 we'll be passed to the number one argument three will be passed to the number two argument and then will be passed to the result in a same way we can define multiple couple values here so the second test is for the string values so the first argument is hello second argument is world and the expected value is this one which is result and the third test case or test scenario is ten point five for the floating values comma twenty five point five and the expected value is 36 now once you have defined that this parameterize decorator you don't need to call this add function multiple times what you need is you just need to define the same variable names which you have defined here in the parameterize so the first argument will be num1 the second argument will be num2 and the third argument will be the result and then you just need to pass these values to the add function so the first number is the first argument the num2 is the second argument and the result is the third argument now what's going to happen when you run the test is this list which you have given here will be iterated over by your parameterize function and these values one by one will be assigned to number one number two and the result and the same value will be passed here as the first argument and the second argument and as the result for this assert statement and one last thing which I'm doing wrong here is I think you need to give these arguments name as the string separated by commas so you just need to give the string separated by these commas so the first argument of this parameter is a string in which you define the arguments which taken by your function to test so num1 and num2 and the result and these arguments are separated by commas and all these values are inside the string so this thing you need to keep in mind that this is a one argument string and inside the string you define those values separated by comma so let's run the code and let's see what happens so you can see now that three tests were run and this first test was with the value 7 and 3 and the expected result was 10 and it passed second you will also see which arguments were passed so in the second test hello and world were passed and the expected result was hello world and in the third test case ten point five and twenty five point five was passed as the arguments and the expected result was 36 so by using this parameter eyes decorator you can just use or call your function which you want to test once and then pass different type of parameters into it using this parameterize decorator so that hey guys welcome to the next video on python unit testing tutorial for beginners using PI test in this video I'm going to show you how to use fixtures with PI test so let's get started so to start with on the left hand side I have this student DB class now as you can see here in the init method I am just initializing a member variable which is data is equal to none and then it has two methods one is connect method and other is get data method now this connect method takes one argument which is data file now if you can see on the right hand side I have written two tests for this student DB and I have first of all imported this class and then I have initialized this class student DB and then I'm calling the connect method here now in the connect method argument I'm giving this data dot JSON as an argument so let me show you what this data dot Jason contains so this data dot jason contains the data of the student so it has this element called students which contains this array which contains different data about different students so right now I have two student data here one is ID one Scot Pass and other is ID to mark and fail so using this connect method I'm just opening this data dot JSON file and then I'm just loading this JSON file as the dictionary so as you can see here I'm using the JSON module which I have imported at the top and whenever I call Jason dot load it's going to convert this JSON dot file into the dictionary and then I have this method called get data which takes one argument which is the name of the student now this name I'm going to search in the dictionary which I got using the connect method and if this name which is provided as an argument is contained in the students data then I'm going to just return this student dictionary so once again as you can see here I'm just importing the student dot DB class and then I'm just initializing the student DB and then I'm just calling the method called connect and in the connect method I'm giving the data dot jason as an argument which is this file which contains the student data and in the next step i am just calling the get data method from the student DB class and i'm giving the name of the students here so you can see the name of the student is caught that's why I am giving Scott here and in the next test I am giving the mark name which is the second name here and then using these statement I am just checking whether this ID which I got using the Scott data is equal to the ID which is there in the JSON file or not same assert I have written for the students name and the students result and similar test I have written for the second name or second student which is mark now as you have already seen to make things simpler I have just used this JSON file as the database file but in real-life situations you might be having some other databases like MySQL or PostgreSQL or MongoDB or any other database and you might want to test your database so the same approach you can use whenever you are trying to use the real database so let's first of all run these tests and let's see what's the result here so I'm going to just run these tests and you can see both tests passed here now let's talk about the problems with these two test cases now you might have already guessed that we are repeating ourselves so we are initializing this database twice for the two tests but let's say we have thousands and thousands of tests then you have to initialize this database thousand times so the first problem is the code repetition the second problem is whenever you want to initialize your database for example thousand time these initializations are resource intensive so they will be costlier to your system on which you are running these test cases because they are consuming your resources so what can be the solution here so you can use two type of solutions in these type of cases the first approach is by using the setup and teardown methods now this setup and teardown method falls into the category of classic xunit style setup if you are familiar with other testing frameworks like unit tests or knows these methods might be familiar to you so first of all we will see how to solve this problem using the setup and teardown methods and then we will see how to solve this problem using PI test fixtures so we already know the problem so to solve this there is a method called a setup and the teardown module method so I'm going to just write setup underscore module and this takes one argument which is module and then here inside this method you can initialize your resources so let me define a global DB variable and I'm going to initialize it with none and then I'm going to just use this initialization inside this setup module so whenever you write the setup module as it is then PI test is going to understand that this is a setup method and it's going to execute this code before executing your tests so what I have done is I have defined the global DB variable that means I need to indicate inside the setup method that this is the global variable and that means now inside this DB we have our database instance so we don't need this initialization in these two test cases now and now we can use this DB instance to call the get data method to get the data about particular student so this is the setup module method there is also a teardown method which we can define here so just write d EF teardown underscore module and then inside the parentheses you give the module as an argument and inside the staredown module you can close your connections or free your resources whatever you want to do after your test you can do in that tear down module so I don't have any clothes method inside this database class so let me create some dummy clothes method here so I'm going to just say def clothes and then I'm going to just use pass here so it's just a blank method and let me just provide the parentheses here so let's say we want to use this clothes method to free our resources or close the connection with the database you can do this by just calling this method so DB dot close and this is going to free your resources and let's just print something yes so we know that these two methods are called so here I'm going to just say that this is a setup method and then inside the teardown module method we are going to just replace this set up by teardown so we know that when this setup method is called and when this teardown method is called so once again this setup module and teardown module will be recognized automatically by your PI test and then this code will be called at the starting of your PI tests and this code will be called at the end after all your tests have been passed or failed then this teardown module tests will be called so let's run the test once again and you will be able to see that this setup method is called first and after the execution of the tests this teardown method is called and we can recognize it by using this print statement now if you haven't already recognized I have used this - s or hyphen s option to allow the print method to execute this print statements so if you want to allow your print statements to be printed on the console you need to use this - s flag to be more clear I'm going to just remove the - B from here so it will be more here so you can see it set up is called first and then you will see two dots here which means two tests has been executed and at the end this teardown method has been called to free any of the resources which you might want to free or close that connection with the database so this is called the setup and teardown method now let's talk about the Python fixtures so instead of using the setup and teardown methods PI tests allows us to write the Python fixtures which will automatically be called at the starting and the end of your tests so instead of writing all this code what I can do here is I have first of all imported this PI test and then I'm going to define a method which I will call it DB because as you can see here all these tests are just using the DB instance so we want to somehow pass this DB instance to our tests and then I'm going to provide these parentheses and inside this method I want to do the same so I will just call this setup code here and let me just remove all this code from here and at the end what I want to do here is I want to just return this DB instance which I got from the student DB class I don't need this global declaration here because there is no global variable and for now let's just comment this code so to declare a fixture you just need to add this decorator at the top of your method which is PI test dot fixture and now what I want to do here is I want to pass this DB instance as an argument of both these test cases so now what will happen is PI tests will recognize that this is your fixture and then whatever you returned from your fixture will be passed using this argument to your test cases so let's run our test and let's see what will happen when we run those tests so you can see both these tests has been passed but you will also observe one more thing and this is that this setup is called twice which is a problem as we have discussed we don't want to call this setup twice we just want to call this setup at the beginning of our tests so how to solve this problem so to solve this problem we can tell PI test that this is a module wide fixture so we just want to call this fixture once at the starting so to do this we just need to write scope is equal to and then just write module here and then we are going to just run our test once again let's see what happens so now you can see the setup is called only once and that's what we wanted right we just wanted to call this setup at the beginning of our test and not at the beginning of each of the tests now let's see how we can add the teardown functionality so I'm going to just use this code after this return and then I'm going to uncomment this code and then let's remove this commented code and to call the staredown code which is written here at the end of your tests what you can do here is you can replace this return by yield so when your test will start it's going to execute the code until yield and at the end of your test this code will be executed so let's see what will happen when we run the code once again so let's just call the PI test once again and you can see setup has been called and at the end of your tests teardown has been called so let me just clear the terminal and let's run this PI test command once again without - vivo bursts so we will be more clear and you can see first of all setup has been called and then your test has been executed which are indicated by these two dots and after your tests this teardown code has been executed so this is how you can use fixtures and setup and teardown methods with your PI test test cases hey guys welcome to the next video in Python unit testing tutorial for beginners using PI test now in this video I'm going to show you how you can run your tests which you have written using PI test inside your PyCharm ide so till now we have been running our tests which we have written using pi tests on the command prompt or the terminal but using PyCharm ide also we can run these tests inside the PyCharm ide so let's see how we can run those tests which we have written using pi test using the pycharm ide so as you can see here on the left hand side i have a class and on the right hand side i have written some tests which are related to this class now i want to run these tests which i have written here using the pi test inside the pycharm ide so for doing this what i need to do is first of all i need to install pi test package inside the PyCharm ide so you can see here when i try to import pi test inside the PyCharm ide first of all it will give me this red squiggly line which means that PI charm is not recognizing this pi test package till now so let's first of all install this package so just click on file and then click on settings and then go to the section with sales project and then your project names so project colon project name so I'm going to click on this and then go to the project interpreter now as you can see here there are some packages which are already here and we want to install a new package which is pi test so I'm going to click on this install or this plus I can hear and then I will just search for pie test and the first option you will see here is pie test and you can see this will be the version which will be installed on our pie charm IDE so what I'm going to do is I'm going to just click on this button let me just move it a little bit above so you can see this button so I'm going to click on this install package button which is going to install PI test package inside my pie charm IDE and once this PI test package is installed you will see this message which says package PI test installed successfully with that green highlighted text that means PI test is installed so you can close this windows I'm going to close this window and you will be able to see this pi test now is added to your project interpreter so now when I click on OK and I will wait for some time till this process has been finished and then after some time you will see this red squiggly line under this PI test is gone so now our pycharm ide recognizes this pi test package and now we want to tell the PI CIAM IDE that we want to run the PI tests so for doing that once again I will click on file and then go to the settings and then this time I will go to that tools so just click on Tools option and when you scroll down you will be able to see this option which says python integrated tools so just click on this and on the right hand side you will be able to see under testing that default test Runner by default is unit tests so we need to change this to PI test ok so just change this default test Runner to PI test and then apply and then click OK which is going to tell pycharm IDE that we want to run the PI tests now as soon as you are finished with the setting of PI test on your PI charm IDE what you can do now as you can right click on the file where you want to run PI test so this is my test underscore math underscore func dot py file where all my tests are there so just right click on this file and then you will be able to see this option which says run PI test in the file you want to run the PI test so just click on this option which says run PI test in the file and as soon as you do this you will be able to see this window which is for the PI test and you will be able to see all the tests which have been passed on the left hand side and the terminal output on the right hand side so let me just maximize this window here and on the right hand side you will be able to see the output so we have run the test on this file and because I have some output which is a print output as you can see here print statement here which is responsible for printing this line and also I have this print statement for teardown so this teardown is printed using this print statement also you will be able to see two dots here which means we have a two tests inside this file which ran successfully so let's add this verbose flag inside our PI tests using PyCharm ide so once you ran your PI test using PyCharm ide you will be able to see this drop down menu here it says pi test in whatever file you are running your PI test on and just click on this file you will be able to see this added configuration option right away after first run of your test so just click on this option which says added configuration and then you can add the arguments which are additional arguments like verbose or - s or whatever option you want to provide here using this additional arguments option so let's I want to provide this - V option which is for wool bows and then I will say apply and then I will say ok and then I'm going to run the test once again either clicking on this button or right away from this button so let me click on this button and once you ran your test you will be able to see the verbose output on the right hand side let me just remove these print statements because they are just not looking good on the tests so I'm going to just remove the setup and teardown print statements from here and I'm going to run the tests once again and then you will be able to see the correct verbose output here now if you want to see all these tests which are passed or failed on the left hand side using these green or red circles then you can click on this green circle at the top here so once click on this green circle and you will be able to see the list of all tests which are there inside your file so this is the script name which is test underscore math underscore func and these are the two tests which are there inside this test script so you can see the first test is test core data which is this one and the second test is test mark data which is the second test here you can also run these tests individually so you can right click and then you can just say run test which is going to run only one test which you want to run so once you ran this only one test you will be able to see on the top that only this tests has been running so in order to run the whole Python script for the test you can once again select this option which is PI test in whatever test script you have so just choose that and then run it once again it will run your all the tests once again and you will be able to see all the tests here so in order to run individual tests you can just right click and then run those tests but remember that you need to change this option again from here also if you want to run the test by just hovering over the test so this is the test let's say I want to run only this test so you can just hover over this test and then right click and then just click on this option run by test for this test which is going to run only that test so once again I'm going to select this option for the whole file which is going to run all the tests once again for the PI test using pycharm ide so this is how you can run your tests which you have written using pi tests using PyCharm ide I hope you have enjoyed this video and I will see you in the next video you
Info
Channel: ProgrammingKnowledge
Views: 115,981
Rating: undefined out of 5
Keywords: PyTest, Python Unit Testing, Unit Testing, Python Unit Test, Getting started with pytest, Unit Testing With PyTest, What is Unit Testing, pytest using pip, pip, unit tests using pytest, pytest tutorial, Py.test, pytest example, Testing Python, pytest setup, PythonCourse, PythonTutorialforBeginners, Python (Programming Language), Python, Programming Language (Software Genre), Python Tutorial, Python course, Python Guru, Learn Python, Python 3, PIP
Id: bbp_849-RZ4
Channel Id: undefined
Length: 63min 19sec (3799 seconds)
Published: Wed Jan 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.