Bird
Raised Fist0
Testing Fundamentalstesting~10 mins

Unit testing in Testing Fundamentals - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple unit test method using Python's unittest framework.

Testing Fundamentals
import unittest

class TestMath(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(2 + 3, [1])
Drag options to blanks, or click blank then click option'
A7
B5
C4
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong expected value like 6 or 4.
Forgetting that assertEqual compares the first and second arguments.
2fill in blank
medium

Complete the code to run all unit tests when the script is executed directly.

Testing Fundamentals
if __name__ == '__main__':
    [1]()
Drag options to blanks, or click blank then click option'
Aunittest.main
Brun_tests
Cmain.unittest
Dtest.run
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not exist like run_tests.
Calling main.unittest instead of unittest.main.
3fill in blank
hard

Fix the error in the test method to correctly check if a list contains an element.

Testing Fundamentals
def test_contains(self):
    items = ['apple', 'banana', 'cherry']
    self.assertTrue([1] in items)
Drag options to blanks, or click blank then click option'
A'grape'
Bbanana
C'banana'
Ditems
Attempts:
3 left
💡 Hint
Common Mistakes
Using banana without quotes causing a NameError.
Checking for an element not in the list like 'grape'.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Testing Fundamentals
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Clen(word) < 3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator like < instead of >.
Mapping word to word instead of word to length.
5fill in blank
hard

Fill all three blanks to create a test that asserts a dictionary comprehension filters positive values correctly.

Testing Fundamentals
result = [1]([2]: [3] for [2] in data if data[[2]] > 0)
Drag options to blanks, or click blank then click option'
Adict
Bk
Cdata[k]
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'v' as the key variable which is undefined.
Not using dict() to convert comprehension to dictionary.

Practice

(1/5)
1. What is the main purpose of unit testing in software development?
easy
A. To deploy the software to users
B. To test the entire software system at once
C. To check small parts of code work correctly
D. To design the user interface

Solution

  1. Step 1: Understand unit testing scope

    Unit testing focuses on testing small, individual parts of code, like functions or methods.
  2. Step 2: Compare with other testing types

    Testing the entire system is integration or system testing, not unit testing.
  3. Final Answer:

    To check small parts of code work correctly -> Option C
  4. Quick Check:

    Unit testing = small code parts check [OK]
Hint: Unit tests check small code parts, not whole system [OK]
Common Mistakes:
  • Confusing unit testing with system testing
  • Thinking unit tests check UI or deployment
  • Believing unit tests run the full application
2. Which of the following is the correct syntax for a simple unit test assertion in Python using assert?
easy
A. assert add(2, 3) == 5
B. assert add(2, 3) = 5
C. assert(add(2, 3) = 5)
D. assert add(2, 3) equals 5

Solution

  1. Step 1: Recall Python assert syntax

    Python uses assert expression where expression must be True for test to pass.
  2. Step 2: Check each option

    assert add(2, 3) == 5 uses correct syntax: assert add(2, 3) == 5. assert add(2, 3) = 5 uses single = which is assignment, invalid here. assert(add(2, 3) = 5) uses single = which is assignment, invalid syntax. assert add(2, 3) equals 5 uses invalid keyword 'equals'.
  3. Final Answer:

    assert add(2, 3) == 5 -> Option A
  4. Quick Check:

    assert with == for comparison [OK]
Hint: Use 'assert expression' with '==' for comparison in Python [OK]
Common Mistakes:
  • Using single '=' instead of '==' in assert
  • Writing assert with invalid keywords
  • Adding unnecessary parentheses in assert
3. Given the Python function and test below, what will be the test result?
def multiply(a, b):
    return a * b

assert multiply(3, 4) == 12
assert multiply(0, 5) == 0
assert multiply(-1, 3) == -3
assert multiply(2, 2) == 5
medium
A. The last assertion fails
B. All assertions pass
C. The first assertion fails
D. The second assertion fails

Solution

  1. Step 1: Evaluate each assertion

    multiply(3,4) = 12 matches expected 12 (pass). multiply(0,5) = 0 matches 0 (pass). multiply(-1,3) = -3 matches -3 (pass). multiply(2,2) = 4 but expected 5 (fail).
  2. Step 2: Identify failing assertion

    The last assertion expects 5 but function returns 4, so it fails.
  3. Final Answer:

    The last assertion fails -> Option A
  4. Quick Check:

    4 != 5 causes failure [OK]
Hint: Check each assert expected vs actual carefully [OK]
Common Mistakes:
  • Assuming all assertions pass without checking values
  • Confusing multiplication results
  • Ignoring the last assertion
4. Identify the error in the following Python unit test code:
def divide(a, b):
    return a / b

assert divide(10, 2) == 5
assert divide(5, 0) == 0
medium
A. The function should return integer only
B. Division by zero causes runtime error
C. The first assertion is incorrect
D. Assertions are missing parentheses

Solution

  1. Step 1: Analyze function behavior

    Function divides a by b. Dividing by zero is not allowed and causes an error.
  2. Step 2: Check assertions

    First assertion divides 10 by 2, which is valid. Second assertion divides 5 by 0, which causes a runtime ZeroDivisionError.
  3. Final Answer:

    Division by zero causes runtime error -> Option B
  4. Quick Check:

    Divide by zero error in second assert [OK]
Hint: Watch for zero division in tests [OK]
Common Mistakes:
  • Ignoring division by zero error
  • Thinking assertions need parentheses in Python
  • Assuming function returns only integers
5. You want to write a unit test for a function that returns the maximum number from a list. Which test case below best checks the function handles an empty list correctly?
hard
A. assert max_number([-1, -2]) == -1
B. assert max_number([1, 2, 3]) == 3
C. assert max_number([0]) == 0
D. assert max_number([]) == None

Solution

  1. Step 1: Understand the edge case

    An empty list has no maximum value, so the function should handle this gracefully, often by returning None or raising an error.
  2. Step 2: Evaluate test cases

    assert max_number([]) == None tests the empty list case expecting None, which is appropriate. Other options test normal lists, not empty.
  3. Final Answer:

    assert max_number([]) == None -> Option D
  4. Quick Check:

    Empty list test expects None [OK]
Hint: Test empty inputs with None or error expected [OK]
Common Mistakes:
  • Not testing empty list edge case
  • Using normal list tests for empty case
  • Assuming empty list returns zero
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