0
0
PyTesttesting~10 mins

Testing multiple exceptions in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to test if a ValueError is raised.

PyTest
import pytest

def test_value_error():
    with pytest.raises([1]):
        int('abc')
Drag options to blanks, or click blank then click option'
ATypeError
BValueError
CKeyError
DIndexError
Attempts:
3 left
💡 Hint
Common Mistakes
Using TypeError instead of ValueError
Not using pytest.raises context manager
2fill in blank
medium

Complete the code to test if a ZeroDivisionError is raised.

PyTest
import pytest

def test_zero_division():
    with pytest.raises([1]):
        result = 10 / 0
Drag options to blanks, or click blank then click option'
AValueError
BOverflowError
CArithmeticError
DZeroDivisionError
Attempts:
3 left
💡 Hint
Common Mistakes
Using ValueError or ArithmeticError instead of ZeroDivisionError
Not using the context manager correctly
3fill in blank
hard

Fix the error in the test to correctly check for multiple exceptions.

PyTest
import pytest

def test_multiple_exceptions():
    with pytest.raises(([1])):
        int('abc')
        result = 10 / 0
Drag options to blanks, or click blank then click option'
AValueError, ZeroDivisionError
BValueError ZeroDivisionError
C[ValueError, ZeroDivisionError]
D['ValueError', 'ZeroDivisionError']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a tuple
Passing exception names as strings
4fill in blank
hard

Fill both blanks to test if either KeyError or IndexError is raised.

PyTest
import pytest

def test_key_or_index_error():
    with pytest.raises(([1], [2])):
        d = {}
        _ = d['missing_key']
Drag options to blanks, or click blank then click option'
AKeyError
BIndexError
CValueError
DTypeError
Attempts:
3 left
💡 Hint
Common Mistakes
Using ValueError or TypeError instead
Not passing a tuple of exceptions
5fill in blank
hard

Fill all three blanks to test if either AttributeError, TypeError, or ValueError is raised.

PyTest
import pytest

def test_multiple_exceptions_complex():
    with pytest.raises(([1], [2], [3])):
        obj = None
        obj.some_method()
Drag options to blanks, or click blank then click option'
AAttributeError
BTypeError
CValueError
DKeyError
Attempts:
3 left
💡 Hint
Common Mistakes
Including KeyError which is unrelated here
Not using a tuple for multiple exceptions