Hello everyone. This is Waseem from Edureka, and I welcome you all to this session in which I am going to talk about pytest in Python. So let us take a look at the agenda for this session. I will start with a brief introduction to what is pytest, and then I will discuss how to get started with pytest. Moving further, we will make the first test with pytest, and then we will take a look at multiple tests and grouping them together into a class, and then we'll be taking a look at parametrized test, and we'll be taking a look at assertions, how to run multiple tests, and Python fixtures, parametrized tests, etc. So I hope you guys are clear with the agenda. Also, don't forget to subscribe to Edureka for more exciting tutorials and press the bell icon to get the latest updates on Edureka. And do check out Edureka's Python programming certification program. The link is given in the description box below. Now, without any further ado, let us understand what is pytest in Python. So what exactly is pytest, guys? The pytest framework makes it easy to write small tests yet scales to support complex functional testing for applications and libraries. Pytest is a mature, full-featured Python testing tool that helps you write better programs. Some of the advantages of using pytest are it is open source first of all, and it can skip test also, automatically detect the test. We can run tests in parallel. You can run a specific test or a subset of tests from your test program. And it is very easy to start with because it is very simple and it has a very easy syntax that you'll take a look at when we're working on the test now. To get started with pytest, you have to install pytest. It is very simple, guys, to install pytest on your system. Basically, you just have to run the following command, which is pip install -U pytest. Or if you're using PyCharm, you can simply go to the project interpreter and add pytest from over there. Now, we'll go ahead and make our first program that is we're going to write our first test using pytest. So let us go to PyCharm, guys. All right. So I'll just delete this. So the project dependencies in this program are very few. So first of all, you have to make sure you have installed pytest on your project. So over here, you can just type pytest. You can see pytest simple powerful testing with Python, and the authors are Holger Krekel, Bruno Oliveira, and all these authors you can find over here like Florian Bruhin and others. And you can simply install this package by clicking on the install package button. And I've already installed this dependency, so I don't have to do that again. And we're going to start with our first test, like the first code that's going to run your test using pytest. So first of all, for that, you have to import pytest, guys, first of all. After this, I'm going to make a function, let's say func and give it some parameter, let's say x. And now I'm going to return x plus let's say five over here. Make another function, and this is my test function, guys. So I'm going to name it as test let's say test_method. And inside this, I'm going to assert or use the assert keyword, and I'm going to check func give it the value three is equal to let's say five. Now to run my method or all these tests that I made over here, I just have to use one command, one simple command that is pytest and my file name, which is first_test.py. And it is showing that test session has started. It is giving collected 1 item, and it has failed because assert 8 is not equal to 5, that is why it has failed. And if I just change this value to let's say 8, and if I run this again now, it should result in 1 passed. So this is how easily you can write test for your any program using pytest in Python, guys. So let us go ahead and talk about assertions in pytest, which are nothing but checks that return either true or false. And if an assertion test fails in a program, the method execution will stop there and it will move to the next test without running the remaining code. Now let us go ahead and take a look at multiple tests and how to run them. So I'll just close this, guys. These are my multiple tests. I'll write it again so that you'll be able to understand this better. So I'll import Okay, I'll just close this, guys. I hope this is visible to you. I'll make it a lot bigger, so that you don't have any problem trying to understand this. For running my multiple tests, first of all, I have to import pytest library. To run the multiple files, we can follow two approaches, which is grouping test names by substring matching and grouping by markers. So I'm going to show you both. Before that, let me just make my test methods. So first of all, I'll just make one. That is test_method1. And after this, I'll give it a few values, let's just say x is equal to let's say 5, and y is equal to let's say 10, and now I'm going to assert if x is equal to y. So this test is definitely going to fail because x is definitely not equal to y. But I've just written it for your understanding, guys. I'm going to make one more method, let's just say method2, and inside this I'm going to just give a few values, let's say a is equal to 15, b is equal to let's say 20. And I'm going to assert a plus let's say 2 is equal to Okay, we want one test to pass, actually, so I'll just write 5 over here, is equal to b. So we have made our tests, guys. So what I'll do now is I'll use the two approaches, which is using the test names by substring matching. So for that, you don't have to do anything, guys. You just have to write pytest, and after this you can just write -k, give the name of your method, and let's say -v. So let's just say we have two files, and we have same method with similar name in both the files, then we can simply use this grouping test names by substring matching, and in this, -k is used to represent the substring to match and -v increases the verbosity. Now I'll just run this. Let's see what happens. So it has deselected 5 in 0.67 seconds, and 1 has failed over here. And instead of method 1, I can just use it for method 2 as well, and it has passed because we have written that test successfully. So we know that it's working fine, and the next approach to run multiple tests is to group them by markers. So to run the test using markers, we have to specify the markers before each test. And now you must be wondering how you do that. So I'll just tell you, guys. Before any test case, you can just write @pytest .let's say mark, and give it any name. I'll just name it as one, let's say. And for this, I'll write pytest .mark.let's say two. Now when I want to call this Now when I want to run these tests, I'll just open the terminal, guys, and for that you have to write pytest -m, and after that you have to mention whatever the marker name is. So I'll just write two first because it ended in success. Yes. And for this, I can just write one as well. So this is how you run multiple tests using pytest in Python, guys. And now that we know how it works, let us go ahead and take a look at pytest fixtures. Oh, before that, I'm let me just show you how you can group all these tests inside a class, guys. So I'll just run a simple program to make you understand this better. So I'll just make a class, guys. Let's just name it as TestClass. After this, let me just give it one function, let's just say I'll just name it as test_one, and let's say x is equal to this or yes, x is equal to let's say edureka, and I want to assert 'r' in x. So this is my one method. After this, I'm going to make one more test method that let's just say test_two. Let's just say x is equal to hello, and I'm going to check or assert hasattr x and let's just say this is check, and now I'll open the terminal and run this using pytest grouping.py. So we have two tests, guys, in this. So test session starts. We have failure for class test 2. Okay, I'll just run it again, guys. So now we are successfully able to run both the tests, and this is how you group your tests in a class. And as you can see, we have one failed and one passed. So the test two has failed, and test one has actually passed. Dot over here symbolizes that it has passed, and F over here symbolizes that the test case has failed. And now that we are done with this, we're going to go ahead and move with fixtures in pytest. So what exactly are fixtures in pytest? Whenever we need to run some code before a test, we use the pytest fixtures. And to use pytest fixtures, we have to mark the test with I'll just show you, guys. So first of all, you have to import pytest. And after this, I'll just write pytest.fixture. And this is the code that will be run before each of my tests. So let me just get a function, let's just say numbers, yes. And inside this, I'm going to let's just say a is equal to 10, b is equal to 20, let's say, and c is equal to 30. Or let's say 25. And I'm going to return a list with a, b, and c. So now I'll make my test case, guys. So my function, let's just say test_method1 numbers, pass numbers here. Okay. So I'll just take x is equal to let's say 15. And I'm going to assert numbers 0 is equal to x. I'll take one more, which is let's just say test_method2 pass numbers here, and let's just say y is equal to 20, and assert numbers 1 or let's just say 2 is equal to y. Which is clearly not the case here, so I'll just make it as 1 so that we have our successful test later. And let's just make one more test case, that is test_method3 pass numbers here, and let's just say z is equal to 25, and assert numbers 2 is equal to z. So these are my test cases. I have made a pytest fixture, so this method is going to be used for all these methods separately. So now I'll open the terminal. Okay, I'll just close this again. I'll open the terminal now and I'll run pytest, and my file name is test_fixtures.py. So we have one failed test and two passed. So test method one failed because assert 10 is not equal to 15, and both the other tests are passed in 0.59 seconds. So this is how we use fixtures in pytest using Python, guys. So after this, I want to talk about parametrized tests. So we can parametrize the test to run multiple arguments for a test. So let us go ahead and take a look at an example to understand that. So first of all, I'll import All right. I'll just make it a little bigger. So first of all, I'll import pytest, and after this @pytest .mark.parametrize, and inside this I'm going to pass a few values, let's just say x, y, and z. And inside the list, I'm going to pass x is equal to 10, let's say, 20, and the output let's say is 200, and we're going to go with 20, 40, and z is equal to let's say 200 again. After this, I'm going to make a function, and inside this function I am going to use let's say this is my test_method, and I'm going to provide all these values, that is x, y, and z, and I'm going to just straightaway give my assert, and I'm going to check for x multiplied by y is equal to z. So now I'm straightaway going to run this test, guys. I'll just close this. pytest test_parametrized, that's the name of my file. We have one failed and one passed. So assert 20 * 40 is not equal to 200, that's why it has failed. Or as well, I can just play with the input values over here. So what I did here is I parametrized these inputs, that is x, y, and z. So basically, x is over here 10 and 20, y is 20 and 40, and z is respectively 200 and 200 over here. And I've used the test method over here, provided these values from using the parametrize, and asserted that x multiplied by y is equal to z. So this is how you can use parametrize. We can use parametrize to run multiple arguments for a test. And this is how easy it as it looks, guys. So now that we are done with this also, okay, I'll just open that fixtures file because we're going to use a few skip methods. I'm going to show you the skip method, guys, or how you can skip the test. For that, you just have to write pytest, that is pytest.mark.skip. Okay, this one has failed, right? So I'll just write xfail over here, and I'm going to skip this one. Okay, I'll just open the terminal. pytest What is the name of file? test_fixture.py. We have one passed, one skipped, and one xfailed. So we have used the skip keyword and the xfail keyword. And using that, we have just skipped one test over here. And we have used the xfail because it will not add into the failed. So this is how you skip a test in your test cases or use the xfail. So it's not going to show failed over here, instead it's going to show xfail. That's both different, guys. And this is how you use skip and xfail, guys. So we're going to work on a use case now. So we're going to use pytest framework for testing an API. So this is the website, guys, where you can actually test your front end against a real API. So it is a hosted API ready to respond to your requests. So I'm going to check for the login one. So for posting a request, we have the email and password over here, and the response is going to be 200, and the token also is given over here. So we'll just write our code. Okay, I'll just close this. Open the test API. I've already written down the code, but I'll write it again for your better understanding, guys. Close this. So I'm going to run the code for pytest framework testing an API. So first of all, I have to import a few dependencies, so that is import pytest first of all. Then I have to import requests and import json as well. So these are the dependencies you need for this. And I'm going to make a let's say main_url, that is going to be the URL that I'm going to be using, and for this I want to return Okay, I'll just copy the URL from over here. What is the URL? reqres.in, and api/login. So I'm going to keep it like this. Now I'll go to my main test function, guys. So I'll name it as let's say test_valid_login, and inside this I'm going to pass my main_url. Now I'll just write url that is going to be used by my function. So main_url plus or append the values that are api and login. So this value Okay, I'll just go back. So api and login. That's it, guys, that's going to be my URL. Now I'm going to look for data. So I'm going to pass email and password. So the valid credentials are this one, and the password is cityslicka. So I'm just going to give random values first of all. After that, I'll try for something else. abc let's say @xyz.com, and I'll give the password. I'll give the password as let's say a random password again. So I'll just write qwerty. All right, so this is my URL and data, so I'm just going to make one response variable now. So in response, we are going to get requests.get, after this I'm going to pass the URL, and the data is equal to data. And now we also have the token to check, so we're going to get the token from json. let's say loads, and over here we want response.text. And after this, we are going to assert a few things. So I'm going to check for response.status_code, and status code, the valid status code is 200, guys. So anything that is not 200, it's going to show that it has failed, or it's not a successful login, guys. And I'm going to assert token for value which is equal to We're going to copy the value from over there which is this. So we have made our program, guys, I'm just going to run this now. So I'll just open the terminal using pytest and test_api.py. So it's showing us an error. So I think I made a mistake, guys. It is showing main_url not found. Or we'll just remove this for now, and instead of that I'm going to give the URL directly, guys. Okay, let me just close this and I'll run it again. Okay, we have our one failed test, guys. I'll just run it again. Let's see what happens now. Just name it as let's say t. So we are getting a KeyError, guys, somehow. So I have changed a few things over here, so it should work now. pytest test_api.py. And it works, guys, because assert 400 is not equal to 200. I think we are getting a response 400, which is not equal to 200, so it's showing it's not a success. So I'll just copy the username and password from over here, and copy the email, and paste it over here. And we'll get the password as well. And this is eve, guys. I forgot to copy the e. I'll run it again, and it should end up in a success. All right, we have one failed. So we have an AssertionError. There has to be a four over there. So I'll just write four. Okay, let me just check the token. Yeah, it has a four over there as well. And I'll run it again. And we have a success, guys. So we have tested our API for a login request where we have provided the email and password. So and we are checking for the status code and the token which is already given over here in the website. So response if it is 200, it's going to show success, and if the token is equal to this, it's going to show no assertion errors over there. So this is how simply you can just use the pytest framework for testing an API. And now that we have come to the end of the session, guys, don't forget to subscribe to Edureka for more exciting tutorials and press the bell icon to get the latest updates on Edureka. So we have created our first test case with pytest, and after that we did few multiple tests where we wrote sample codes, and using pytest, we ran the test cases. Then we grouped the multiple test cases inside a class, and we talked about assertions and parametrized test, and then we also took a look at pytest fixtures and how we can skip the test. And last but not the least, we worked on a pytest framework for testing an API. And don't forget to check out Edureka's Python programming certification program, guys. The link is given in the description box below. Thank you, and happy learning. I hope you have enjoyed listening to this video. Please be kind enough to like it, and you can comment any of your doubts and queries, and we will reply them at the earliest. Do look out for more videos in our playlist, and subscribe to Edureka channel to learn more. Happy learning!