title: Is a postcondition a (type of) unit test?
body: <p>I'm trying to incorporate some design-by-contract techniques into my coding style.  Postconditions look a lot to me like embedded unit tests and I'm wondering if my thinking here is on the right track or way off-base.</p>
  
  <p>Wikipedia defines a postcondition as "a condition or predicate that must always be true just after the execution of some section of code or after an operation in a formal specification. Postconditions are sometimes tested using assertions within the code itself".  </p>
  
  <p>Is that not very similar to what you do in a unit test that verifies state directly (doesn't use mocks)?  </p>
  
  <p>If that's the case:</p>
  
  <p>1) By using post-conditions, aren't I now sort of embedding testing code in my production code, and isn't that frowned upon?</p>
  
  <p>2) Should using postconditions change the structure of my unit tests? My first thought is that the assertion logic is moved from the tests to the postconditions.  That is, tests will use the same inputs and I'm still testing everything I was testing before, but now instead of making assertions in the unit tests I'm making a simple binary assertion about the postconditions passing or not.</p>
  
  <p>3) My second thought is that postcondition code might have control flow and is therefore not ideal for test code, which is supposed to be simple and avoid control flow.  But, if I test the postconditions, can I then rely on them in my unit tests?</p>
  
  <p>4) It seems difficult to test postconditions because if I understand them correctly they basically pass or fail and you would have to repeat the logic of the postcondition itself to check that it did the right thing.  So, how do you test a postcondition?  Do you check them by not utilizing them in your unit testing and ensuring your unit tests and postconditions pass or fail together?</p>
  
  <p>5) My unit tests sometimes verify that a method has caused changes to state in collaborators.  In standard practice, do postconditions cover collaborator state or just the state of the class they are defined on?</p>