0
0
Testing Fundamentalstesting~10 mins

Unit testing in Testing Fundamentals - Interactive Code Practice

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