Positive vs Negative Tests
Explore the differences between positive and negative unit tests and understand how they work together to verify that application code behaves as expected. Learn when to apply negative tests alongside positive ones to cover unexpected inputs and ensure thorough testing of business rules and methods.
Introduction
A natural inclination when writing test cases is to check that a test case behaves as expected with expected input. This is only one side of the coin when writing a test case. To write a test case that comprehensively checks the behavior of a method, we need to check that a method behaves as expected with unexpected input. A positive case tests whether the code behaves like it should. A negative case tests whether the code does not behave like it shouldn’t. These two terms are very difficult to define and are best demonstrated by example.
Differences between positive and negative tests
The best way to demonstrate this concept is to use an example of a human resources department approving leave applications. Suppose that only the human resource manager may approve leave applications. The positive test ...