Unit testing anti-patterns: Structural Inspection
Although I don&#x27;t disagree that the testing of structure in the example given is not particularly good practice, I disagree with the overall message.<p>There is a difference between &quot;unit tests&quot; and &quot;acceptance tests&quot;.  &quot;Does this test verify some business requirement&quot; is a question aimed at the latter, not the former.  There is nothing wrong with writing automated acceptance tests (and I highly encourage it), but they are not a replacement for unit tests.<p>What are unit tests for?  They are for helping you reason about the effect of changes on your code.  There are two main questions a unit test should answer. 1: What happens when I do X (where X is calling some code)? 2: Does the behaviour change if I modify the production code?<p>The first question is for helping you write the code in the first place (or for modifying it subsequently).  It is not a question of the application operating properly, it&#x27;s a question of the &quot;unit&quot; operating the way you expect.  You should be able to use this information to reason quickly about what went wrong in the case that behaviour in the system changes unexpectedly: &quot;Unit a&quot; should behave like X, and I can see that it does so by looking at the tests.  If that&#x27;s the case, then &quot;Unit y&quot; must be at fault.<p>You can think of these kinds of tests as being sort of like predefined watch points in your debugger.  You should expose state in your units and add tests so that you can understand the state of the unit if it doesn&#x27;t behave as expected.  You should be thinking, &quot;If the behaviour of this unit is unexpected, what things would I want to look at when determining why&quot;.<p>You might be wondering (as many do) what a &quot;unit&quot; is.  The answer is that it can be anything: a function, a class, a global object, a group of classes&#x2F;objects acting in concert.  It really doesn&#x27;t matter.  Some people like to fix the idea of a &quot;unit&quot; as a class and also add &quot;integration tests&quot;.  I have no problem with that, other than I have seen no real value in the practice.  Mostly I think people find it easier to envision how to name the files that contain their tests.<p>Many styles of testing do a relatively good job of doing what I describe above.  However many people isolate their &quot;units&quot; too much.  I think this is an unfortunate consequence of the word &quot;unit&quot; which seems to imply &quot;atomic unit&quot; to many people.  The word (IMHO) refers more to simply an identifiable chunk of code.  Often people avoid using real objects as collaborators in their tests in the mistaken belief that it will help with the specificity of their tests.<p>The second question, &quot;Does the behaviour change if I modify the production code&quot; is as important as the first question.  Imagine a scenario where you have a series of either manual or automated acceptance tests.  You run those tests and you feel confident that the application is running as you expect.  You then modify some code.  At this point, it would be very nice to be told what behaviour (if anything) has changed in the application.<p>You might be forgiven for thinking that&#x27;s the same as a regression test.  However, it is not necessary to actually write regression tests to give you that information.  We don&#x27;t actually need to know whether or not the behaviour is correct -- we only need to know if it is different (and ideally, what is different).<p>As an example, I once worked on code that converted files to and from MS Word file format.  I didn&#x27;t write any of the original code and it was pretty stinky, TBH.  Every time I made a change, I would likely break something because I couldn&#x27;t reason about the code.  However, we had about 2000 documents that QA had already tested and deemed acceptable.  I wrote a quick hack that would allow me to compare the &quot;accepted&quot; output for these documents with the new outcomes.  It didn&#x27;t try to understand <i>what</i> was wrong, just that it was different.  In fact, most of the time the changes were <i>good</i> because I was improving the conversions.<p>Obviously, the above is not a unit test system.  However, the point is that we don&#x27;t need to write complicated tests to determine if the system is working correctly.  All we need is to have a known baseline for the system and then be alerted when the behaviour changes -- either in a &quot;good&quot; or &quot;bad&quot; way.  Then we can have a quick look to see if the change in behaviour was what we expected.<p>In that way, the second criteria for good unit tests is that at least one unit test &quot;fails&quot; when I change the behaviour of the system.  A lot of people equate TDD (Test Driven Development) with Test First Development.  This is unfortunate because it steers people off course.  Test First is a good way to learn TDD, but it&#x27;s not by any stretch of the imagination the only way to do TDD.  When I have a piece of code with good unit tests, I will often modify production code first and see what tests fail.  If I have changed behaviour and no tests fail, I know I&#x27;m in big trouble.  Similarly, if 200 tests fail and I have no specificity in my tests, I know I&#x27;m going to be in for a hard ride.  These are the kinds of things you should be doing to test the quality of your tests.<p>I&#x27;ll finish this with one last thought.  There are 2 classes of &quot;customer&quot; for tests: the end user and the programmer.  The user is concerned about whether or not the application is working as desired.  They don&#x27;t care what is technically wrong and they don&#x27;t need to reason about how to change unexpected behaviour.  The programmer doesn&#x27;t actually need to know what the correct behaviour is for everything in the system.  They need to have a known baseline of behaviour and to be told when the behaviour has changed.  They also need to know if the very specific piece of code that they are currently working on is behaving as expected.  The customer for unit tests are the programmer, not the end user.  Having tests for the end user is a great idea, but doesn&#x27;t actually help tremendously with improving the development experience.  The golden rule for programming tests as a programmer: if it helps you reason about what you are doing, then it&#x27;s a good idea.  If it doesn&#x27;t, then you should avoid it.