0
0
PyTesttesting~20 mins

First PyTest test - Practice Problems & Coding Challenges

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!
Predict Output
intermediate
2:00remaining
What is the output of this simple PyTest test?
Consider this PyTest test function. What will be the test result when you run it?
PyTest
def test_sum():
    assert 2 + 3 == 5
ATest fails with AssertionError
BTest passes successfully
CSyntaxError occurs due to missing colon
DRuntimeError due to undefined variable
Attempts:
2 left
💡 Hint
Check if the assertion condition is true or false.
assertion
intermediate
2:00remaining
Which assertion will correctly check if a list contains exactly 3 items?
You want to write a PyTest assertion to verify that the list items = [1, 2, 3] has exactly 3 elements. Which option is correct?
Aassert len(items) == 3
Bassert items.count == 3
Cassert items.length() == 3
Dassert items.size == 3
Attempts:
2 left
💡 Hint
Use the built-in function to get the number of elements in a list.
🔧 Debug
advanced
2:00remaining
Why does this PyTest test fail?
Look at this test code and find why it fails when run with PyTest.
PyTest
def test_divide():
    result = 10 / 0
    assert result == 0
ASyntaxError due to missing colon
BAssertionError because result is not zero
CZeroDivisionError occurs before assertion
DTest passes because 10 divided by 0 is 0
Attempts:
2 left
💡 Hint
What happens when you divide by zero in Python?
framework
advanced
2:00remaining
Which PyTest feature allows running setup code before each test function?
You want to run some code before every test function in a file. Which PyTest feature should you use?
AUse a main() function to call setup manually
BUse a class with __init__ method for setup
CUse a global variable initialized outside tests
DUse a fixture with scope='function' and the @pytest.fixture decorator
Attempts:
2 left
💡 Hint
PyTest fixtures are designed for setup and teardown.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using PyTest over basic assert statements in scripts?
Why do testers prefer PyTest framework instead of just writing assert statements in Python scripts?
APyTest provides detailed test reports, fixtures, and easy test discovery
BPyTest runs code faster than plain Python scripts
CPyTest automatically fixes bugs in the code
DPyTest requires no installation and works out of the box
Attempts:
2 left
💡 Hint
Think about what features a testing framework adds beyond simple asserts.