

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to group test cases in Pytest?
We can group test cases in Pytest. Pytest is a test framework in python. To install pytest, we need to use the command pip install pytest. After installation, we can verify if python has been installed by the command pytest – version. The version of pytest shall be known.
Pytest can be used for creating and executing test cases. It can be used in a wide range of testing API, UI, database, and so on. The test file of pytest has a naming convention that it starts with test_ or ends with _test keyword and every line of code should be inside a method that should have a name starting with test keyword. Also, each method should have a unique name.
In order to print the console logs, we need to use the command py.test –v –s. Again, if we want to run tests from a specific pytest file, the command is py.test <filename> -v.
Pytest gives the feature of markers on the test methods. The marker is used to give properties or attributes to the test methods. Some of the default markers are skip, xfail, and parametrize. Besides, there can be more customized markers as per our needs.
The markers are associated with the test method with the following syntax− @py.test.mark.<markername>. Also to use markers, we have to import pytest to our test file. To execute the marked test methods, the command is py.test -m <markername> -v.
Here -m <markername> is the name of the marker and v means verbose.
Example
Let us consider a pytest file having test methods.
import pytest @pytest.mark.loan def test_CalculateLoan(): print("Loan calculation") def test_CalculateLease(): print("Lease calculation")
Let us consider another pytest file having test methods.
import pytest @pytest.mark.loan def test_CalculateRepay(): print("Loan calculation") def test_FindLease(): print("Lease search")
To run a group of test methods having a user-defined mark as loan, the command should be py.test -m loan –v. In our example test methods having mark as loan, shall be chosen for execution. In this case, the CalculateLoan() and the CalculateRepay() will be executed.
- Related Questions & Answers
- How to Write Test Cases?
- How can a particular group of test cases get executed in TestNG?
- How to exclude a test from execution in Pytest?
- How to run multiple test cases using TestNG Test Suite in Selenium?
- How to skip a selected test from execution in pytest?
- How to skip a selected test from execution in pytest?
- How to run Selenium WebDriver test cases in Chrome?
- How to set priority to the test cases in TestNG?
- How to include and exclude test methods from a set of test cases in Cucumber?
- How to group selected tests from a set of tests in pytest?
- How to group selected tests from a set of tests in pytest?
- HealthCare Domain Testing with Sample Test Cases
- Payment Gateway Testing with Example Test Cases
- Cookie Testing: Example Test Cases for Website
- What is Configuration Testing? Example Test Cases