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.
Latihan ini adalah bagian dari kursus
Introduction to Testing in Python
Petunjuk latihan
- Add the "conditional skip" decorator to make it work.
- Add the
condition_stringinto the decorator call. - Complete the assertion tests.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
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]