Itβs true that using Contracts.Requires and Contracts.Ensure in C# methods will reduce the necessary unit tests for that methods? Can I just ignore the range of values that are not in conformity with the contracts or those values should also be included in the unit tests?
Asked
Active
Viewed 121 times
1 Answers
2
It should not, design by contract is not intended to replace unit testing, or any kind of testing. Pre-conditions and post-conditions are used to enforce a contract, but the end client of that contract needs to make sure that such contract is still in place. So you need to keep the unit tests in place with all range of values to make sure everything is the way it should be.

lpaneque
- 36
- 3
-
You are saying that we should only use contracts if we are implementing an API that will bu user by another person or team? β miguelbgouveia Apr 21 '15 at 16:43
-
3not *only*, if you like DbC, then use it (all the time if you want), and its a good practice to use it. But it does not replace any type of testing, however it can help the testing-coding process since you can know in advance what its expected of it. β lpaneque Apr 21 '15 at 18:26
-
1By following DbC, it's (more) _possible_ to write code such that callers of your code "fall into the pit of success"--in other words, you can as much as possible, make it hard for callers to call library methods incorrectly. Having said that, this is not a silver bullet--it's just another tool. I once wrote a module using contracts, but I mis-typed the contract pre-condition. Boy was I surprised when my unit tests failed! I don't need to test that Code Contracts works properly, but I do need to test that my fingers typed out the right contract! β fourpastmidnight Feb 20 '16 at 06:19