Test your knowledge of turning software bugs into minimal reproducible cases and writing failing unit or integration tests—with mocks and stubs—to validate bug fixes and prevent regressions. Perfect for anyone seeking to strengthen their debugging and testing skills.
Identifying Minimal Reproducible Cases
Why is creating a minimal reproducible case important when you discover a bug in your code?
- It speeds up program execution.
- It provides a detailed manual for users.
- It guarantees the bug will not return.
- It isolates the bug for easy testing and debugging.
Writing a Unit Test for a Bug
What is the main purpose of writing a failing unit test after discovering a bug in a function that calculates totals incorrectly?
- To make the test suite run slower.
- To check if test frameworks are installed correctly.
- To confirm the bug exists and will be fixed by the code change.
- To automatically document the code.
Difference Between Mock and Stub
What is the typical difference between a mock and a stub when testing functions that depend on external services?
- A mock provides random outputs and a stub always throws errors.
- A mock only works for APIs, a stub for databases.
- There is no difference; the terms are interchangeable.
- A mock verifies calls and a stub provides predefined responses.
Preventing Regressions
How does turning a discovered bug into an automated test help prevent regressions in software development?
- By disabling other tests temporarily.
- By hiding the bug from users.
- By alerting developers if the bug reappears after future code changes.
- By rewriting all code automatically.
Failing Test Scenario Example
If a function should return 0 for empty inputs but returns null instead, why is writing a failing test for this case valuable?
- It makes code look more complex.
- It increases code compilation speed.
- It hides implementation details from the team.
- It demonstrates the incorrect behavior explicitly.
Unit vs. Integration Test for Bug Reproduction
When is it more appropriate to use an integration test instead of a unit test to reproduce a bug?
- When the bug is in a single isolated function.
- When only documentation is affected.
- When the bug involves multiple interacting components.
- When testing only for performance.
Best Practices in Minimizing Test Cases
What is a best practice when creating a minimal reproducible test case for a bug in a sorting algorithm?
- Use as much real data as possible for every test.
- Include unrelated code to ensure full coverage.
- Reduce the input data to the smallest example that still reproduces the issue.
- Ignore edge cases in the test case.
Benefits of Automated Regression Tests
Which statement best describes the benefit of keeping previous failing bug tests in your automated test suite?
- They help ensure that fixed bugs do not reoccur after new code changes.
- They encourage manual testing only.
- They slow down the testing process intentionally.
- They replace all documentation needs.
Mocking Behavior in a Test
In a test for an email sending function, why might you use a mock object instead of sending real emails?
- To test multiple programming languages at once.
- To simulate external behavior without causing real-world effects.
- To increase network traffic during testing.
- To generate random errors intentionally.
Test Fails Before Fix
What should happen to the failing unit test you wrote for a bug once you apply a code fix?
- The test should now pass if the bug is fixed correctly.
- The test should be deleted after the fix.
- The test should be renamed and kept failing.
- The test should always fail, even after the fix.