Challenge - 5 Problems
Unit Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple unit test with assertion
What is the result of running this Python unit test code?
Testing Fundamentals
import unittest def add(a, b): return a + b class TestAdd(unittest.TestCase): def test_add_positive(self): self.assertEqual(add(2, 3), 5) if __name__ == '__main__': unittest.main(exit=False)
Attempts:
2 left
💡 Hint
Check if the add function returns the sum correctly and the assertion matches.
✗ Incorrect
The add function returns the sum of two numbers. The test checks if add(2, 3) equals 5, which is true, so the test passes.
❓ assertion
intermediate1:30remaining
Choosing the correct assertion for checking list length
Which assertion is correct to verify that a list named 'items' has exactly 4 elements in a unit test?
Attempts:
2 left
💡 Hint
Think about how to check the number of elements in a list.
✗ Incorrect
len(items) returns the number of elements. Using assertEqual compares this number to 4, which is the correct way to check list length.
🔧 Debug
advanced2:30remaining
Identify the error in this unit test code
What error will this Python unittest code raise when run?
Testing Fundamentals
import unittest def multiply(x, y): return x * y class TestMultiply(unittest.TestCase): def test_multiply(self): self.assertEqual(multiply(3, 4), 12) def test_multiply_fail(self): self.assertEqual(multiply(2, 2), 5) if __name__ == '__main__': unittest.main(exit=False)
Attempts:
2 left
💡 Hint
Check the expected value in the failing assertion.
✗ Incorrect
The test_multiply_fail expects multiply(2, 2) to be 5, but multiply(2, 2) returns 4, causing an AssertionError.
🧠 Conceptual
advanced1:30remaining
Purpose of mocking in unit testing
Why do testers use mocking in unit tests?
Attempts:
2 left
💡 Hint
Think about testing parts of code without relying on external systems.
✗ Incorrect
Mocking replaces real parts with fake ones so tests focus on the unit itself without outside influence.
❓ framework
expert2:00remaining
Understanding test discovery in Python unittest
Given this directory structure and files, which command correctly discovers and runs all tests?
project/
tests/
test_math.py
test_string.py
app.py
Options:
Attempts:
2 left
💡 Hint
Check the unittest module's discover command syntax.
✗ Incorrect
The unittest module's discover command with -s for start directory and -p for pattern correctly finds test files.