Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

How to skip a particular test using pytest in Python

Ravi

Introduction to pytest

A Python testing framework called pytest makes it easy and simple to create small, understandable tests and has the capacity to handle complex functional testing for programmes and modules.

Pytest markers

One of the important functionalities provided by pytest is markers. Markers are used to set metadata, such as, different characteristics and properties to test functions. The markers are attached to a test function in the form of function decorators. There are some inbuilt markers like xfail, skip and so on.

The skip is one such marker provided by pytest that is used to skip test functions from executing.

The syntax to use the skip mark is as follows:

@pytest.mark.skip(reason="reason for skipping the test case")
def test_case():
    ....

We can specify why we skip the test case using the reason argument of the skip marker.

Code example

Le’s look at the code below:

import pytest

@pytest.mark.skip(reason="Dependent feature not yet ready")
def test_new_feature():
    print("New feature")

def test_login():
    print("Test Login")
Try it out!

Code explanation

  • Line 1: We import pytest.
  • Lines 4 and 5: We call the test function and define test_new_feature(). This function is annotated with the skip marker decorator with the reason as Dependent feature not yet ready.
  • Lines 7 and 8: We define a test function called test_login().

In the output, we can observe that test_new_feature() test case is skipped while test_login() is executed.

RELATED TAGS

python
RELATED COURSES

View all Courses

Keep Exploring