Welcome back. In this lecture we are going to discuss the concept of fixtures. According to pi test documentation page. The purpose of test fixtures is to provide a fixed baseline upon which tests can be reliably and repeatedly executed. And if we scroll down below, fixtures allow test functions to easily receive and work against specific pre initialized application objects without having to care about import set up clean up details. So in short, you can consider fixtures as being ways of setting the prerequisites and building an initial setup for all your tests.
Fixture functions are registered by marking them with at pi test that fixture and that's introducing a concept we've discussed earlier in the course decorators. We are basically decorating this function right here. For example, which becomes the so called fixture function. Now let's see the code for this lecture. This is the file right here x and y dot p y. This file is saved inside a new folder called fixtures on my D drive.
By the way, as with all the other files in this section, you can download the code from the notebook following this video. So let's open up this file in Notepad plus plus. First of all, we need to import the necessary modules for this application. Nothing new here since we've already used these modules throughout this section up until this point, next we have two functions. As you can see right here, the fixture function and the test function down below. The fixture function is called x y func.
And as you can notice, it's been decorated using the add pi test dot fixture decorator in order to specify that this is the fixture to use for any upcoming tests. Unlike in the previous video, notice two things here. First, the function to be tested and the test function are both inside the same file this time. Secondly, notice that the fixture function contains most of the functionality, the loading of the Excel file as a panda's data frame, the definition of the x value and also the simulation of the users input using unit test dot mock and the final value of 10. Finally, the same mathematical expression is performed using the values of x and y. So this function contains all the necessary prerequisites that the test function needs in order to evaluate the result against a given value, which is again 894.
Next, the test function called test underscore result takes the fixture function as a parameter, as you can see right here, rounds up the results returned by the x, y function to the nearest integer and finally used Is the assert statement to compare the two values. As you can notice the use of fixture functions enhances the modularity of the code, and also reduces the size of the test functions by setting the initial conditions and providing the necessary data that the tests need in order to be properly executed. Instead of the current contents of our x, y func function. Imagine that maybe you can initialize the connection to a remote database server, or load the data scraped from a website as prerequisites for your test with. Finally, let's open up the windows command line. And let's run our file or test and see if everything runs properly using the new concept of fixtures being implemented in our code.
So I'm going to move over to the D drive. And now CD, pictures, sorry, pictures, Enter. And now by test dash, dash, disable that Warning to suppress any unwanted warning messages. And finally, the name of our file x and y dot p y enter. So as you can see, our test has been performed and the result is the one we expected, which is best known as the green dot. Now let's move on to the next lecture and talk more about fixtures and add more complexity to our test setup.
So see you in the next one.