Python Unit Testing With Pytest 3 - Parameterizing tests (pytest.mark.parametrize)



 hey guys welcome to the next blog on Python unit testing tutorial for beginners using PI tests in this blog 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 module and then above my test function I will just call PI tests first of all then dot mark dot parameterize so we are testing this add 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 our 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 one argument two 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 will be passed to the number one argument 3 will be passed to the number 2 argument and then will be passed to the result in a same way we can define multiple tuples here so the second test is for the string values so the first argument is hollow second argument is world and the expected value is this one which is result and the third test case or test scenario is 10.5 for the floating values , 25.5 and the expected value is 36 now once you have defined 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 parameterize is string in which you define the arguments which are 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 seven and three and the expected result was ten 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 rise 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's it for this blog I will see you in the next blog 

Post a Comment

Previous Post Next Post

Recent in Technology News