How to skip a selected test from execution in pytest?


We can skip a selected test from execution 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 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 becomes irrelevant due to a known bug in the application or a particular feature is still in development. The pytest test framework gives the option of skipping test methods from execution.

The skipping markers are associated with the test method with the following syntax − @py.test.mark.skip. Also to use markers, we have to import pytest to our test file. Once the test methods become relevant, we need to remove the skip mark from the test method.

Let us consider a pytest file having test methods.

import pytest
@pytest.mark.loan
@pytest.mark.skip
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.skip
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 skip, shall be excluded from execution. In this case, the CalculateLoan() and the CalculateRepay() test methods will be skipped from execution.

Updated on: 29-Jul-2020

319 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements