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”
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]