Python Unit Testing With Pytest 4 - pytest fixtures + setup/teardown methods

 


hey guys welcome to the next blog on Python unit testing too tall for beginners using PI test in this blog 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 Scott 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 am going to search in the dictionary which I got using the connect math third 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 JSON as an argument which is this file which contains the student data and in the next step I'm 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 assert 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 student's 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 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 teardown module so I don't have any close method inside this database class so let me create some dummy closed method here so I'm going to just say def closed 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 closed 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 set up method is called and when this teardown method is called so once again this setup module and teardown module will be recognized automatically by our PI test and then this code will be called at the starting of your PI and this code will be called at the end after all your tests have been passed or failed then this teardown module test 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's setup 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 that 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 me instance which I got from the student TB 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 test will recognize that this is your fixture and then whatever you return 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 the 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 yeah 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 burst 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 I hope you have enjoyed this blog and I will see you in the next blog  

Post a Comment

Previous Post Next Post

Recent in Technology News