A blog about the joys and perils of software development

Was This Test Written to Specify or Fixate Behavior?

When you read unit test code, have you ever wondered why some tests tell a story (easy to read) while others just feel mechanical (more computer readable than human).

While I have no definitive answers on the human/non-human readable unit tests subject, I have an insight that came to me and a colleague some time ago: Some tests are written as a specification, and some tests are written to fixate. Let me explain what I mean.

When you write a test as a specification, the test code is normally written upfront or together with the code. The purpose of the test code is to document the behavior of the code. Thus, the developer makes an effort to make the test code human-readable. The tests are often used as an acceptance criteria, to verify a piece of functionality was implemented correctly. The fact that you get an executable specification is a great bonus, since the test code would be valuable just as an example.

Other tests are written to fixate the existing behavior of the code. Typical examples are tests written long after the production code in order to cover legacy code with tests. Legacy code can be hard to test, so the test code is difficult to write in human-readable way and tends to be more “mechanical” in nature. The focus of these tests is execution, since the purpose is to make sure refactoring does not break existing behavior.

Naturally, we want our test code to lie in the specification category. But if we have a piece of code that is really hard to test, we can’t just start refactoring blindly. So we write the best tests we can, and refactor the code and tests in parallel. After some work, the production code is in better shape and we have moved our test code into the specification category.

When you write test and production code from scratch, you have every opportunity to write good tests. Make sure you make your code human-readable. The computer will do just fine. :)

This entry was posted in Software Development and tagged , , , . Bookmark the permalink.

9 Responses to Was This Test Written to Specify or Fixate Behavior?

  1. Pär says:

    Amen! Well said. Kudos. And so forth

  2. Alejandro says:

    Wow, I feel like I just answered the same 5 qiostuens in 15 different ways. Do you like being alone? Do you like being alone on weekdays? Do you like being alone at home? Would you rather go to a party or be alone? Yes yes, I’m an introvert, I know.Other than being an introvert, I’m a whole lot like Bunnies4Buddha, which explains why our conversations tend to be a lot of Me too! and I know, I feel exactly the same way! and I totally understand. The site doesn’t have the most flattering description of ISTJ’s not spontaneous, not adventurous. In other words, boring! Oh, but dependable. Great.It also makes a big deal about how we’re punctual, which I’m totally not. Heh.

  3. That’s an inventive answer to an interesting question

  4. I believe that is among the such a lot important information for me. And i am happy studying your article. But want to commentary on few common things, The site style is perfect, the articles is in point of fact excellent . Good process, cheers.

  5. Tour: Giveaway: H.A. Caine’s Second Time Around WINNER One week ago I participated in a Blog Tour for H.A.Caine’s new short story/novella, Second Time Around. H.A. was kind enough to allow me the opportunity to also giveaway a PDF copy

  6. I got what you mean , thankyou for putting up.Woh I am glad to find this website through google. “The test and use of a man’s education is that he finds pleasure in the exercise of his mind.” by Carl Barzun.

  7. I have got one idea for your web page. It looks like right now there are a number of cascading stylesheet issues while launching a number of webpages within google chrome and opera. It is operating alright in internet explorer. Perhaps you can double check this.

Leave a Reply

Your email address will not be published.