IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Introduction to Testing in Python

Visualizza il corso

Istruzioni dell'esercizio

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

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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]
Modifica ed esegui il codice