Operating with subtests
In TDD, the test scope should be small, and the outcome should be easy to understand. We explored TDD best practices in Chapter 1, Getting to Grips with Test-Driven Development. To achieve these best practices, we need separation between test scenarios.
Let’s consider the example of the TestAdd
function that we have worked with so far. It currently tests adding two positive numbers, but we will extend it to cover negative inputs. With the knowledge we have so far, we have two options:
- Extend the scope of
TestAdd
to verify the new scenario: This approach will make the Assert step longer, but it has the advantage of reusing the previous steps. - Create a new test to verify the new scenario: This approach will keep the scope of
TestAdd
as is, but it has the disadvantage of us having to redefine and re-execute the Arrange and Act steps of the existing test.
If we choose to go with the second option, we will have to name the new test something...