Why Pytest?
I started my Python adventure with UI test automation using Selenium and Behave which I also wrote an article about it previously. And now when I need an API test automation I prefer to continue using Python since I discover how easy and fun to use it. But I’m not going to talk about how good Python is. I will share my research and experience with Pytest in this article.
When we decided to have an API test automation for our project in Beyn Technologies, I started to prepare a test automation plan and make some research for what to use. I tried to find the best possible tool to use which gains the most for the test automation project. This is where test automation planning is important to have before starting. This is also one of the subjects of my previous articles if you need to check it.
It is where I met with Pytest. After all the research I compared tools that I can use. Of course, they were python test frameworks since I was determined to use Python. I compared tools to use and they were Behave, which I have experienced before, Robot framework, and Pytest.
In this article, I will talk about what is Pytest, and why I preferred to use it.
What is Pytest?
Pytest is a Python-based test framework that is used to write and execute codes. It is mostly used for API testing but it is much more than that where you can write code for simple cases and more complex test cases. We can use Pytest to write API tests and besides we can code tests for database and UI tests.
Advantages of Pytest
I need to know what are the advantages of all the tools I compared and see which is better. I found Pytest very useful for API test automation for the following positives:
- Pytest has its own way to detect the test file and test functions automatically, if not mentioned explicitly.
- It allows us to execute a specific part of a test suite and also skip a subset of the entire test suite.
- Pytest can run multiple tests in parallel, so it reduced the execution time of the test suite significantly.
- It has a simple syntax and is easy to learn and start using.
- It is open source and free.
- It has many documentation available and IDE support.
Comparison with Robot Framework
I was not sure which framework is better for our purpose since both the Robot framework and Pytest are frequently used in test automation and have their own advantages. I found a nice resource during my research that compares Pytest and Robot frameworks and helps me to which one to choose.
Robot Framework is an open-source, keyword-driven test automation framework. The test cases in Robot Framework are user-oriented and are not too technical. It is the biggest advantage of the Robot framework that allows you to automate tests using keywords. This framework is mostly used as less technical skill is required as compared to programming-language-based frameworks like Pytest. Therefore, It can be used by team members that have less experience in programming.
Pytest is mostly used for API testing, also we can use Pytest for simple as well as complex tests as we mentioned before. We use Pytest via the command line and it has a nice ability to detect the tests written automatically, the only thing you should do is to name the test cases with the prefix ”test” and it will execute the test cases and will get the results.
Pytest is flexible and has fixtures that make it simple to define test cases that will be executed as scope based. Pytest is very smart and it captures the log messages automatically and displays them for each failed test case in the same manner as captured. By default, for each captured message Pytest displays a line of error, method name, module, and message.
Why I preferred using Pytest?
We have an API test automation to start and it seemed Pytest is preferably more suitable for the aim of our project.
The Robot framework seems very efficient to organize and write test cases whereas Pytest has more efficient for writing repeated test cases. In our API automation project, we have many game providers and most of the test cases have repeated actions. Therefore Pytest seemed more suitable for our project as the key difference between Robot and Pytest frameworks.
The most important reason why I preferred Pytest is it is better to build complex automation. It also has static code analysis and huge IDE support.
In the project, we have many game providers to write code for API test automation and it keeps growing with newly implemented providers in time. If we add our test automation to the project pipeline in the future, it will take time to run all the test automation suites. So, I thought the ability to run tests in parallel with Pytest can be useful for us and it is the final reason why Pytest is better for our project.
In conclusion, I think the most important thing while selecting tools and frameworks is your needs during project implementation. If you don’t trust your programming skills you can prefer the Robot framework as it is easy to use because of its keyword-driven approach. But If you have good programming skills and want to build complex automation, then you should go for Pytest which is also preferred to use in our automation project. I plan to mention how to start using Pytest and usage examples in the following articles. Happy testing!