0
0
PyTesttesting~20 mins

Why PyTest is the most popular Python testing framework - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PyTest Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Key feature that makes PyTest popular
Which feature of PyTest most contributes to its popularity among Python developers?
AIts ability to run tests without requiring boilerplate code like classes or main functions
BIt requires writing tests in a special PyTest language
CIt only supports testing of web applications
DIt only works with Python 2.x versions
Attempts:
2 left
💡 Hint
Think about how easy it is to write tests quickly with PyTest.
Predict Output
intermediate
2:00remaining
Output of a simple PyTest test function
What will be the result when running this PyTest test function?
PyTest
def test_sum():
    assert sum([1, 2, 3]) == 6
ASyntaxError due to missing colon
BTest fails with AssertionError
CRuntimeError because sum is undefined
DTest passes successfully
Attempts:
2 left
💡 Hint
Check the sum of the list [1, 2, 3].
assertion
advanced
2:00remaining
Correct assertion to check exception in PyTest
Which PyTest assertion correctly checks that a ValueError is raised when calling int('abc')?
PyTest
import pytest

def test_int_conversion():
    # Fill in the assertion here
    pass
AassertRaises(ValueError, int, 'abc')
Bassert int('abc') == ValueError
C
with pytest.raises(ValueError):
    int('abc')
D
try:
    int('abc')
except ValueError:
    pass
Attempts:
2 left
💡 Hint
PyTest uses a context manager to check exceptions.
🔧 Debug
advanced
2:00remaining
Identify the error in this PyTest test
What error will PyTest report when running this test code?
PyTest
def test_divide():
    result = 10 / 0
    assert result == 0
AAssertionError because result is not 0
BZeroDivisionError at runtime
CSyntaxError due to missing colon
DTest passes successfully
Attempts:
2 left
💡 Hint
What happens when dividing by zero in Python?
framework
expert
3:00remaining
PyTest fixture scope behavior
Given a fixture with scope='module', how many times will it run if 3 test functions in the same module use it?
PyTest
import pytest

@pytest.fixture(scope='module')
def resource():
    print('Setup resource')
    yield
    print('Teardown resource')

def test_one(resource):
    pass

def test_two(resource):
    pass

def test_three(resource):
    pass
AOnce before all three tests, then teardown after all tests
BBefore and after each test function (3 times total)
COnly once before the first test, no teardown
DOnce before the first test and once after the second test
Attempts:
2 left
💡 Hint
Module scope means the fixture runs once per module.