Bird
Raised Fist0
PyTesttesting~20 mins

First PyTest test - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does PyTest look for to identify a test function?
easy
A. Functions decorated with @pytest.test
B. Functions ending with _test
C. Functions starting with test_
D. Functions containing assert keyword

Solution

  1. Step 1: Understand PyTest naming convention

    PyTest automatically finds functions whose names start with test_.
  2. Step 2: Compare options with PyTest rules

    Only functions starting with test_ are recognized as tests by default.
  3. Final Answer:

    Functions starting with test_ -> Option C
  4. Quick Check:

    PyTest test function = starts with test_ [OK]
Hint: Look for functions starting with test_ to find tests [OK]
Common Mistakes:
  • Thinking functions ending with _test are tests
  • Believing assert keyword alone marks a test
  • Assuming decorators are needed for basic tests
2. Which of the following is the correct syntax for a simple PyTest test function?
easy
A. def add_test():\n assert 1 + 1 == 2
B. def test_add():\n assert 1 + 1 == 2
C. def test_add():\n assert 1 + 1 == 3
D. def testAdd():\n assert 1 + 1 == 2

Solution

  1. Step 1: Check function name format

    PyTest requires test functions to start with test_, so test_add is correct.
  2. Step 2: Verify assertion correctness

    The assertion 1 + 1 == 2 is true, so the test will pass.
  3. Final Answer:

    def test_add():\n assert 1 + 1 == 2 -> Option B
  4. Quick Check:

    Correct test name and true assert = def test_add():\n assert 1 + 1 == 2 [OK]
Hint: Use def test_...() with assert for valid PyTest tests [OK]
Common Mistakes:
  • Naming function without test_ prefix
  • Using incorrect assertion values
  • Using camelCase instead of snake_case for test names
3. What will be the result when running this PyTest test?
def test_example():
    x = 5
    y = 2
    assert x - y == 3
medium
A. Test passes successfully
B. SyntaxError occurs
C. Test fails with AssertionError
D. Test is skipped automatically

Solution

  1. Step 1: Evaluate the assertion expression

    Calculate x - y which is 5 - 2 = 3.
  2. Step 2: Compare with asserted value

    The assertion checks if 3 == 3, which is true, so no error is raised.
  3. Final Answer:

    Test passes successfully -> Option A
  4. Quick Check:

    5 - 2 equals 3 means assert passes [OK]
Hint: Calculate assert expression; true means test passes [OK]
Common Mistakes:
  • Assuming test fails without checking assertion
  • Confusing syntax errors with assertion failures
  • Thinking tests skip without skip decorators
4. Identify the error in this PyTest test function:
def test_sum():
    total = 2 + 2
    assert total = 4
medium
A. Using single equals (=) instead of double equals (==) in assert
B. Function name does not start with test_
C. Missing return statement in test function
D. Indentation error in function body

Solution

  1. Step 1: Check the assert statement syntax

    The assert uses = which is assignment, not comparison. It should be ==.
  2. Step 2: Confirm function name and indentation

    The function name starts with test_ and indentation is correct, so no error there.
  3. Final Answer:

    Using single equals (=) instead of double equals (==) in assert -> Option A
  4. Quick Check:

    Assert needs ==, not = [OK]
Hint: Use == in assert, not = for comparison [OK]
Common Mistakes:
  • Using = instead of == in assert
  • Thinking return is needed in test functions
  • Confusing indentation errors with syntax errors
5. You want to test a function multiply(a, b) that returns the product of two numbers. Which PyTest test function correctly tests that multiply(3, 4) returns 12?
hard
A. def test_multiply():\n assert multiply(3, 4) == 7
B. def test_multiply():\n assert multiply(3, 4) = 12
C. def multiply_test():\n assert multiply(3, 4) == 12
D. def test_multiply():\n assert multiply(3, 4) == 12

Solution

  1. Step 1: Verify test function name

    The function name must start with test_, so test_multiply is correct.
  2. Step 2: Check assertion correctness

    The assertion must compare with == and the expected result is 12, so assert multiply(3, 4) == 12 is correct.
  3. Final Answer:

    def test_multiply():\n assert multiply(3, 4) == 12 -> Option D
  4. Quick Check:

    Correct name and assert with == 12 = def test_multiply():\n assert multiply(3, 4) == 12 [OK]
Hint: Test functions start with test_ and assert expected == actual [OK]
Common Mistakes:
  • Wrong expected value in assert
  • Wrong function name prefix
  • Using = instead of == in assert
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard