By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Unit Testing with unittest – TestCase, Assertions is a fundamental practice in software development that checks individual units of source code to confirm they work as intended. Real-world importance? It catches bugs early, saving time and money. In exams, it's a critical topic, often comprising 10-15% of questions. Get it wrong, and you risk deploying buggy software, leading to costly fixes and potential system failures. For instance, a banking application with flawed unit tests might allow incorrect transactions, causing financial loss.
Action: Import the unittest module.Principle: Access the testing framework.Example:
import unittest
Pitfall: ⚠️ Forgetting to import can lead to undefined references.
Action: Define a class that inherits from unittest.TestCase.Principle: Organize related tests within a class.Example:
unittest.TestCase
class TestStringMethods(unittest.TestCase): pass
Pitfall: ⚠️ Not inheriting from TestCase will result in undefined test methods.
TestCase
Action: Add methods to your TestCase class that start with test.Principle: Each method represents a single test.Example:
test
def test_upper(self): self.assertEqual('foo'.upper(), 'FOO')
Pitfall: ⚠️ Methods not starting with test won't be recognized as tests.
Action: Use assertion methods to check conditions.Principle: Verify that the code behaves as expected.Example:
self.assertEqual(a, b) # Checks if a equals b self.assertTrue(x) # Checks if x is True self.assertRaises(exc, func, *args, kwds) # Checks if func raises exc
Pitfall: ⚠️ Incorrect assertion can lead to false positives or negatives.
Action: Use setUp and tearDown methods for pre- and post-test actions.Principle: Prepare the test environment and clean up afterward.Example:
setUp
tearDown
def setUp(self): self.widget = Widget('The widget') def tearDown(self): self.widget.dispose()
Pitfall: ⚠️ Skipping teardown can leave resources allocated.
Action: Use unittest.main() to run the tests.Principle: Automate the execution of all test cases.Example:
unittest.main()
if __name__ == '__main__': unittest.main()
Pitfall: ⚠️ Running tests manually can be error-prone and time-consuming.
Experts view unit testing as a continuous quality assurance process. They focus on writing small, isolated tests that can be run frequently. Instead of waiting for integration tests to catch bugs, they use unit tests to verify each component independently. This mindset leads to more reliable and maintainable code.
Why it's wrong: Integration tests are slower and harder to debug.How to avoid: Focus on testing individual functions or methods.Exam trap: Questions that mix integration and unit testing concepts.
Why it's wrong: Incorrect assertions can lead to false test results.How to avoid: Understand and use the appropriate assertion method.Exam trap: Choosing the wrong assertion method in multiple-choice questions.
Why it's wrong: Tests may interfere with each other, leading to inconsistent results.How to avoid: Always use setUp and tearDown methods.Exam trap: Questions that require identifying the cause of test failures.
Why it's wrong: Missing important feedback on test failures.How to avoid: Always review test output and logs.Exam trap: Questions that ask for the interpretation of test results.
Question: Write a unit test for a function that adds two numbers.Solution: 1. Import unittest.2. Create a TestCase class.3. Write a test method using assertEqual.4. Run the tests.Answer:
assertEqual
import unittest def add(a, b): return a + b class TestAddFunction(unittest.TestCase): def test_add(self): self.assertEqual(add(1, 2), 3) if __name__ == '__main__': unittest.main()
Why it works: The test verifies that the add function correctly adds two numbers.
add
Question: Write a unit test for a function that raises an exception.Solution: 1. Import unittest.2. Create a TestCase class.3. Write a test method using assertRaises.4. Run the tests.Answer:
assertRaises
import unittest def divide(a, b): if b == 0: raise ValueError("Cannot divide by zero") return a / b class TestDivideFunction(unittest.TestCase): def test_divide_by_zero(self): self.assertRaises(ValueError, divide, 1, 0) if __name__ == '__main__': unittest.main()
Why it works: The test confirms that the divide function raises a ValueError when dividing by zero.
divide
ValueError
Question: Write a unit test that uses setup and teardown methods.Solution: 1. Import unittest.2. Create a TestCase class.3. Define setUp and tearDown methods.4. Write a test method.5. Run the tests.Answer:
import unittest class Widget: def __init__(self, name): self.name = name def dispose(self): self.name = None class TestWidget(unittest.TestCase): def setUp(self): self.widget = Widget('Test Widget') def tearDown(self): self.widget.dispose() def test_widget_name(self): self.assertEqual(self.widget.name, 'Test Widget') if __name__ == '__main__': unittest.main()
Why it works: The test prepares the environment before each test and cleans up afterward.
unittest
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.