IniziaInizia gratis

pytest.raises

In this exercise, you will continue writing the first test suite using the pytest library.

The function multiple_of_two checks whether the num is a multiple of 2 or not. In this exercise, you will implement a test that expects to raise an Exception.

The pytest package has been imported.

Questo esercizio fa parte del corso

Introduction to Testing in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Define a context manager for the exception test.
  • Write a test to check that the zero input multiple_of_two(num=0) results in the ValueError exception.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

def multiple_of_two(num):
    if num == 0:
        raise(ValueError)
    return num % 2 == 0

def test_zero():    
  	# Add a context for an exception test here
    ____ ____(ValueError):
      	# Check zero input below
        multiple_of_two(____)
Modifica ed esegui il codice