Get Started

Conditional skipping

Sometimes, you want to skip a test if some condition is met. For example, you want to conduct a test unless today is Saturday. In this case, you can use the datetime library for getting the current day of the week and the pytest marker for conditional skipping the test function. To pass a condition to the conditional skipping pytest marker, you can use @pytest.mark.skipif(condition) The pytest library was already imported for you.

This is a part of the course

“Introduction to Testing in Python”

View Course

Exercise instructions

  • Add the "conditional skip" decorator to make it work.
  • Add the condition_string into the decorator call.
  • Complete the assertion tests.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

day_of_week = datetime.now().isoweekday()

def get_unique_values(lst):
    return list(set(lst))

condition_string = 'day_of_week == 6'
# Add the conditional skip marker and the string here
____.____.____(____)
def test_function():
	# Complete the assertion tests here
    ____ ____([1,2,3]) == [1,2,3]
    ____ ____([1,2,3,1]) == [1,2,3]

This exercise is part of the course

Introduction to Testing in Python

AdvancedSkill Level
4.2+
6 reviews

Master Python testing: Learn methods, create checks, and ensure error-free code with pytest and unittest.

Learn what a test is and how to run the first one of your own with the pytest library! You will get used to the pytest testing framework and the command-line interface. You will also learn how to process specific contexts, like "failed tests" and "skipping the test" with pytest markers.

Exercise 1: Introduction to Testing in PythonExercise 2: The first test suiteExercise 3: pytest.raisesExercise 4: Invoking pytest from CLIExercise 5: Run the test!Exercise 6: Run with the keywordExercise 7: Applying test markersExercise 8: Markers use casesExercise 9: Failed tests with xfailExercise 10: Conditional skipping

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free