TDD Cycle Steps
Which of the following represents the correct sequence of steps in the TDD cycle when developing a new feature?
- Red, Green, Refactor
- Green, Red, Refactor
- Refactor, Red, Green
- Red, Refactor, Green
- Green, Refactor, Red
TDD for Bug Fixes
When fixing a bug in an existing codebase using TDD, what is the best first step to take?
- Write a failing test that exposes the bug
- Delete the buggy function entirely
- Deploy immediately to production
- Refactor the code before adding any tests
- Add a comment explaining the bug
Test Specificity
What is the benefit of writing a specific, focused test for each feature or bug in TDD?
- It makes it easier to pinpoint failures when bugs are introduced
- It increases the number of lines of code unnecessarily
- It hides bugs by spreading them across multiple files
- It ensures code runs faster by skipping tests
- It reduces the need for documentation
Interpreting a Failing Test
You added a new feature and run your test suite, seeing the following test failure message: 'Expected output: 5, Actual output: 7'. What should you do next according to TDD principles?
- Fix the implementation so the test passes
- Remove the test since it fails
- Deploy the code anyway
- Comment out the failing line in the test
- Write manual test cases instead
Testing for Edge Cases
Why is it important to include edge cases when writing tests in TDD?
- Edge cases can reveal bugs that normal inputs might not catch
- Edge cases slow down development unnecessarily
- It is required by all testing frameworks
- They always fail and should be avoided
- Edge cases make code less readable
Refactoring Safely
How does TDD help ensure that refactoring existing code doesn’t introduce new bugs?
- Existing tests confirm that code changes do not break expected behavior
- It skips testing altogether
- Refactoring is discouraged when using TDD
- TDD only works for new features, not refactoring
- It encourages merging untested code
Recognizing a Good Test
Which characteristic does NOT describe a good unit test in the context of TDD?
- Fragile and dependent on unrelated code
- Clear assertion for expected behavior
- Can be run automatically and quickly
- Tests one concept or behavior at a time
- Repeatable result regardless of order
TDD in Legacy Code
You’re tasked with fixing a bug in legacy code that currently has no tests. Which is the best first step using TDD?
- Write a test that reproduces the bug before fixing it
- Fix the bug and avoid tests since the code is old
- Rewrite the entire codebase
- Add comments describing the bug
- Ignore the bug and focus on new features
Identifying Flaky Tests
After adding new tests for a recently added feature, you notice some tests sometimes pass and sometimes fail without code changes. What is this scenario called?
- Flaky tests
- Memory leaks
- Optimized tests
- Redundant assertions
- Coverage overflow
TDD Preventing Bugs
How does practicing TDD help reduce the number of bugs when adding new features?
- By forcing you to define expected behavior before writing implementation
- By allowing you to skip regression testing
- By focusing only on manual testing
- By reducing the time spent on code
- By encouraging hardcoding values