0
0
Testing Fundamentalstesting~20 mins

Unit testing in Testing Fundamentals - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unit Testing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ATest passes with no errors
BTest fails with AssertionError
CSyntaxError due to missing colon
DTypeError because add expects strings
Attempts:
2 left
💡 Hint
Check if the add function returns the sum correctly and the assertion matches.
assertion
intermediate
1: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?
Aself.assertIs(items, 4)
Bself.assertTrue(items == 4)
Cself.assertEqual(len(items), 4)
Dself.assertIn(4, items)
Attempts:
2 left
💡 Hint
Think about how to check the number of elements in a list.
🔧 Debug
advanced
2: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)
ATypeError because multiply expects strings
BNo error, all tests pass
CSyntaxError due to missing parentheses
DAssertionError in test_multiply_fail
Attempts:
2 left
💡 Hint
Check the expected value in the failing assertion.
🧠 Conceptual
advanced
1:30remaining
Purpose of mocking in unit testing
Why do testers use mocking in unit tests?
ATo replace real dependencies with controlled fake objects for isolated testing
BTo speed up the program by skipping all function calls
CTo automatically generate test cases without writing code
DTo convert unit tests into integration tests
Attempts:
2 left
💡 Hint
Think about testing parts of code without relying on external systems.
framework
expert
2: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:
Apython -m unittest run tests
Bpython -m unittest discover -s tests -p 'test_*.py'
Cunittest run tests/
Dpython app.py --run-tests
Attempts:
2 left
💡 Hint
Check the unittest module's discover command syntax.