- 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 run a test method without reporting as passed or failed in pytest?
We can run a test method without reporting as passed or failed in pytest. This test method is generally used as a prerequisite. 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 wide range 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 which 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 need.
There may be situations in which a test method is failing due to a known bug in the application or a particular feature is still in development. But that test method cannot be excluded from the run since it has some preconditions. The pytest test framework gives the option of running a test method without reporting as passed or failed with the help of xfail marker.
The xfail marker is associated with the test method with the following syntax − @py.test.mark.xfail. Also to use markers, we have to import pytest to our test file.
Let us consider a pytest file having test methods.
import pytest @pytest.mark.loan @pytest.mark.xfail 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 @pytest.mark.xfail def test_CalculateRepay(): print("Loan calculation") def test_FindLease(): print("Lease search")
Now to run all the test cases, we need to use the command py.test –v. The test methods having mark as xfail, shall be included in the execution. In this case, the CalculateLoan() and the CalculateRepay() will be included in the run but after execution the pass/fail result shall not be reported.
- Related Articles
- How to run a selected test from a set of tests in pytest?
- How to group test cases in Pytest?
- How to exclude a test from execution in Pytest?
- How to skip a selected test from execution in pytest?
- How to execute a selected test from a collection of tests in Pytest?
- How to get the name of a test method that was run in a TestNG teardown method?
- How to run test groups in TestNG?
- How will you run a prerequisite method and post condition method for every test in TestNG?
- How to run multiple test classes in TestNG?
- How to run multiple test cases using TestNG Test Suite in Selenium?
- How to run Selenium WebDriver test cases in Chrome?
- How to run precondition and postcondition test methods in Cucumber?
- How to run selenium (Firefox) web driver without a GUI?
- How to run tests using a test runner file for Cucumber?
- How to run a method every 10 seconds in Android?
