hey guys welcome to the next blog on Python unit testing tutorial for beginners using PI test in the last blog we were able to create our unit test and run them using PI test now in the last blog I have created two tests and these were the tests I have written for these two simple functions which were product and the add function so in the last blog 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 functions 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 function 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 am 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 the 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 keyboard 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 pi 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 minus V 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 ad keyword or ad Tring 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 am going to run the test it's going to run all those tests which contains either add or the string keyword so there are three tests which fulfills this condition and all those three tests are performed and other tests will be D selected 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 and 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 executed and the other three tests 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 write 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 PyCharm 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 tests using command line so this will work so don't worry about this 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 and test product is for the numbers so we can write number here and also we can add the same decorator for that 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 ENTER and it's showing me four D 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 mark so those two tests were executed and other two tests were D 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 exist 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 then 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 fail so here you can just write - - max fail option and then gave the number in front of it after the equals to symbol so let's say we just write max fail to 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 field one 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 this 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 three point three 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 mad 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 three point three which I have I have the Python version three point seven 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 yeah 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 pie 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 I hope you have enjoyed this blog and I will see you in the next blog
Post a Comment