- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 execute a selected test from a collection of tests in Pytest?
We can execute a selected test from a collection of tests 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.
Let us consider a pytest file having test methods.
def test_CalculateLoan(): print("Loan calculation") def test_CalculateLease(): print("Lease calculation")
Let us consider another pytest file having test methods.
def test_CalculateRepay(): print("Loan calculation") def test_FindLease(): print("Lease search")
To execute the test methods having a particular string within its name, we need to execute the command, pytest -k <substring> -v. Here -k <substring> is the substring to look for in the test methods and v means verbose.
For our case, the command should be pytest -k Calculate –v. The test methods having Calculate in their names, shall be chosen for execution. In this case, CalculateLoan(), CalculateLease()and CalculateRepay() will be executed.
- Related Articles
- How to run a selected test from a set of tests in pytest?
- How to group selected tests from a set of tests in pytest?
- How to skip a selected test from execution in pytest?
- How to exclude a test from execution in Pytest?
- How to group test cases in Pytest?
- How to execute a single test from a large testing suite using TestNG.xml?
- How to test and execute a regular expression in JavaScript?
- How to run a test method without reporting as passed or failed in pytest?
- How to run tests using a test runner file for Cucumber?
- How to retain elements from a Collection in another Collection
- How to remove all elements from a Collection in another Collection
- How to run a specific group of tests in TestNG from command line?
- How to execute a particular test method multiple times (say 5 times) in\nTestNG?
- How to set Test and Collection Runner in Postman?
- How to incorporate and remove test methods from execution from a\ncollection of test cases in TestNG?
