30 Mar PyTest vs Robot – Which automation testing framework to choose?
Are you confused about which framework to start within python?
Let’s understand what these frameworks are and how to choose between them with their usage.
Robot Framework is an open-source, keyword-driven test automation framework for Acceptance Testing and Acceptance Test-Driven Development (ATDD). A keyword-driven test approach means capabilities implemented in python can be extended by its test libraries.
Acceptance Testing with Robot Framework:
The test cases in Robot Framework are user-oriented and are not too technical. That is, it has an easy-to-use syntax for test data that allows you to automate tests using keywords.
Acceptance Test-Driven Development:
Acceptance test-driven development (ATDD) is an important agile practice merging requirement gathering with acceptance testing. It is simple enough so that also non-programmers can create and understand test cases.
Why Robot Framework?
This framework is mostly used as a less technical skill is required as compared to programming-language based frameworks Therefore, Can be used by team members that have less experience in programming.
For example, Product owners could express acceptance tests using the framework, without having to know the details of how the product is implemented.
Also, a well experienced technical person can also write keywords in their language of choice to test small functions as for unit testing and integration testing.
Some brief useful points for Robot Framework:
- Because of its keyword-driven approach, Robot Framework can be used to test the following application:
a. Desktop applications
b. Web applications
c. Mobile applications, and SOAP-based services.
- It is possible to write tests that work cross-platform (ie: the same test case can be used to test both an android and iOS application, or for testing a web application that can be executed on Google Chrome, Mozilla Firefox, and Safari).
- One of the great strengths of the robot framework is that it is highly extensible.
a. You can plug in a library in Pytest to use selenium to run a browser.
b. You can plug in a database library to directly access databases.
Let’s take a simple example of robot framework with python:
- Create a python file.
- Install selenium and import web driver as your package.
- Initialize the driver according to your browser, Like If you want to run google chrome then download chromedriver separate exe file to control your browser.
- Create a method and write your code inside it.
Goto your robot file:
We use Settings in Robot Framework to import the file in which you have written the function(example:assert.py)like above. With the name abc, Where abc is a keyword used to call the method.
Then, We implement Test Cases to call the method using the keyword declared in Settings.
Pytest is a testing framework based on Python. Pytest is now popular because it’s easy-to-use fixtures. Pytest is mostly used for API testing, also we can use Pytest for simple as well as complex tests, that is, you can write test cases to test APIs, database, etc.
As Pytest is a command-line tool it detects the tests written automatically, just we have to name the test cases with a prefix test and it will execute the test cases and will get the results.
Why Pytest for writing functional API tests:
Fixtures in Pytest makes it simple to define the test cases that will get executed as they are scope based. Custom fixtures are created in a way that they can be easily reused by each new package, the method we add. You can execute the test cases as per session, module, method just QA have to specify the scope of it.
Fixtures parametrization is another feature that helps us send parameters used during fixtures execution. Parameters in Pytest are added as annotations – pytest.use.fixture, pytest.mark.parameterize.
Pytest is pretty smart, 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.
- The Pytest package must be installed in your IDE.
- Your test case file and your test case must start with a prefix test. So to automatically detect by Pytest for executing it.
Let’s understand with simple example:
import pytest @pytest.fixture def test(username, password): x = username y = password obj = cal(x, y) return obj def test_incorrectusername(test): response = login(test.a, test.b) assert response == "Login Failed" def test_incorrectpassword(test): response = login(test.a, test.b) assert response == "Login Failed"
To execute the above code:
- Go to the terminal of your py file having the above code.
- Go to the directory file having your test case file.
- Type Pytest and press enter to execute the test file.
The above example shows how with a single fixture data inputs can be fetched to perform login operation.
Let’s understand the key difference between Pytest and Robot:
- In terms of test data organization, Robot Framework is an efficient way to organize the test cases and write the test cases in a very handy manner. Whereas for Pytest, It is an efficient way of clubbing the repeated test cases in a single test function and variables can be passed with the use of fixtures, As explained in the above example.
- If we go in terms of code maintainability, Pytest will be preferred than Robot as all the fixtures are present in the Pytest file and also it removes the compulsion of config for each suite.
- With Pytest, Execution time for test cases is reduced to 30-40 percent as compared to the Robot Framework.
- Robot Framework provides HTML reports whereas Pytest does not, you have to install a plugin in Pytest to get the detailed report.
According to me, If you are a beginner having less experience in the automation domain, then you should go for Robot Framework as it is easy to use because of its keyword-driven approach and rich in-built libraries. For detailed information refer: https://robotframework.org/
But If you have a good programming skill and want to build complex automation, then you should go for Pytest as it also comes with static code analysis, huge IDE support, etc
For detailed information refer: https://docs.pytest.org/en/latest/index.html
I hope this will help you out to choose the framework according to your requirements.