hey guys welcome to this first blog 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 tests 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 appear 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 manner 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 - Kay or - Kay it's going to match sub-string or the regular expression which you provide after this flag and when you use - em 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 the 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 Matt 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 keyboard 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 the 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 we are expecting the result 14 here once we have written our unit tests let's try to run these tests using PI tests 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 funk dot py is there and test underscore math underscore funk 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 matte underscore funk 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 it's going to fail you can see one passed and one failed and pi test 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 - 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 + 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 test so let's say we just write tes underscore ad 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 math 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 or 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 that's it for this blog in the next coming blogs we will talk more about PI tests and how to use PI tests so stay tuned and please keep watching these blog I will see you in the next blog
Post a Comment