Another way of marking test functions is by using custom markers, which are markers that are defined by the user. To demonstrate this, I have created a new folder on my D drive called custom marking. And inside this folder, I saved the same calculator.py application from the previous video, and the slightly modified version of the test module we used for analyzing built in markers. You can download the code for this lecture from the notebook that follows or you can write the code yourself while watching this video. Now, let's open up this calculation dot p y. First of all, notice that I have renamed two of the test functions.
These ones right here to test underscore lower value and test underscore higher value and you're going to understand why very soon. Also, I have removed the markers that we've discussed in the previous video. I've added a custom marker to each of the first three functions. As you can see right here, the custom marker is called my test. And consequently, I've decorated each test function using at pi test.mark.my test, pretty easy to define your own markers right. Now, why would you want to use custom markers in your code?
Well, simply because you may want to run only certain tests from this test suite. And using a custom marker is a great and simple way of referencing the desired tests at runtime. For example, I want to run only these three functions right here. When executing this test module. I can easily reference them by specifying the custom marker like this. So the CD custom marking and now I'm going to use PI test dash v dash dash disable warning dash m test, which is the marker, enter.
Great, so only the three tests I needed were executed. Actually, you can also notice that we have five tests that were collected from the test module, but only three of them have been selected. On the other hand, maybe I need to run all the tests except these three tests right here. To do that, I'm going to simply use the not keyword in the command line. So I'm going to use dash m, and in between double quotes, not my test. Sorry, my test, enter.
Okay, notice that this time we have three tests deselected the ones I marked with my custom marker, and two test being executed and passed. Pretty cool. The last thing I'll show you here is how to run certain tests based on their names or parts of their names. For this, we are going to need them The dash k option when using the PI test command, the dash k option will only run the tests which match a given substring expression. Let's see this in practice. Let's say that I want to run only the tests that have the substring lower in the name, I would then use the following command by test dash v dash dash disabled warning, dash K, lower, enter.
Okay, so this time from the five tests inside the module, four of them have been deselected since they don't match the naming criteria, and only the test underscore lower function has been executed as expected. On the other hand, if you need to run all the tests in the test module, except the ones matching a certain substring in their name, then you can use the not keyword here as well. So let me prove this to you. Dash K, not lower Okay, so this time, you can notice that the test underscore lower value function has been skipped. And all of the other tests in the module have been executed because their names do not include the specified substring. Pretty nice, pretty straightforward and definitely very useful.
I hope you've enjoyed this lecture as well and I will see you soon.