Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does “DAMP not DRY” mean when talking about unit tests?

Tags:

unit-testing

I heard someone say that unit tests (e.g. nUnit, jUnit, xUnit) should be

DAMP not DRY

(E.g. unit tests should contain "damp code" not "dry code")

What are they talking about?

like image 729
Ian Ringrose Avatar asked Jun 23 '11 11:06

Ian Ringrose


People also ask

Should unit tests be DRY?

Since tests don't have tests, it should be easy for humans to manually inspect them for correctness, even at the expense of greater code duplication. This means that the DRY principle often isn't a good fit for unit tests, even though it is a best practice for production code.

What does damp testing stand for?

DAMP stands for "Descriptive and Meaningful Phrases" and promotes the readability of the code. People often put these two principles in opposition to each other saying that: You can't apply both DRY and DAMP to the same piece of code.

Should unit tests be self contained?

Every single unit test should be self-contained and not depend on others. Don't repeat yourself.

What is DRY in software engineering?

The DRY (don't repeat yourself) principle is a best practice in software development that recommends software engineers to do something once, and only once.


2 Answers

It's a balance, not a contradiction

DAMP and DRY are not contradictory, rather they balance two different aspects of a code's maintainability. Maintainable code (code that is easy to change) is the ultimate goal here.

DAMP (Descriptive And Meaningful Phrases) promotes the readability of the code.

To maintain code, you first need to understand the code. To understand it, you have to read it. Consider for a moment how much time you spend reading code. It's a lot. DAMP increases maintainability by reducing the time necessary to read and understand the code.

DRY (Don't repeat yourself) promotes the orthogonality of the code.

Removing duplication ensures that every concept in the system has a single authoritative representation in the code. A change to a single business concept results in a single change to the code. DRY increases maintainability by isolating change (risk) to only those parts of the system that must change.

So, why is duplication more acceptable in tests?

Tests often contain inherent duplication because they are testing the same thing over and over again, only with slightly different input values or setup code. However, unlike production code, this duplication is usually isolated only to the scenarios within a single test fixture/file. Because of this, the duplication is minimal and obvious, which means it poses less risk to the project than other types of duplication.

Furthermore, removing this kind of duplication reduces the readability of the tests. The details that were previously duplicated in each test are now hidden away in some new method or class. To get the full picture of the test, you now have to mentally put all these pieces back together.

Therefore, since test code duplication often carries less risk, and promotes readability, its easy to see how it is considered acceptable.

As a principle, favor DRY in production code, favor DAMP in test code. While both are equally important, with a little wisdom you can tip the balance in your favor.

like image 115
Chris Edwards Avatar answered Oct 12 '22 14:10

Chris Edwards


DAMP - Descriptive And Meaningful Phrases.

"DAMP not DRY" values readability over code re-use. The idea of DAMP not DRY in test cases is that tests should be easy to understand, even if that means test cases sometimes have repeated code.

See also Is duplicated code more tolerable in unit tests? for some discussion on the merits of this viewpoint.

It may have been coined by Jay Fields, in relation to Domain Specific Languages.

like image 35
Dominic Rodger Avatar answered Oct 12 '22 14:10

Dominic Rodger