0

Do you think it is advisable to use Code Contracts inside your unit tests? I don't think it is since it is like creating unit tests to test your unit tests. Therefore I would not activate the Code Contracts Static Checking nor the Runtime Checking for a Unit Test project.

To clarify, I would like to know, in particular, if you would write Code Contracts code inside your unit tests.

Luca Cremonesi
  • 144
  • 2
  • 3
  • 13

2 Answers2

1

I think it's a good idea to enable Code Contracts for unit tests, then any contract failures can be caught when the tests are run.

However it's usually not useful to do any contract checking in the test methods themselves, except in helper methods used by the test methods.

Keith
  • 20,636
  • 11
  • 84
  • 125
0

Improved testability

•each contract acts as an oracle, giving a test run a pass/fail indication.

•automatic testing tools, such as Pex, can take advantage of contracts to generate more meaningful unit tests by filtering out meaningless test arguments that don't satisfy the pre-conditions.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
  • I'm asking if I should put Code Contracts code inside my unit tests. The link you reported does not seem to consider this case. It just states that having Code Contracts in your **production** code improves its testability. Therefore it makes sense, as @Keith was saying, to activate the Static Checker in the Unit Test project. – Luca Cremonesi Apr 02 '15 at 17:25