C# Unit Testing – Should Simple Unit Test Projects Be Documented?

cunit testing

I have read questions like this:
Are unit tests really used as documentation?

With regards to code comments; my research so far is telling me:

1) Some developers do not like any code comments and prefer to read unit test code method names to understand the code.

2) Some developers prefer code comments and unit test method names to understand the code

I have used the Sandcastle documentation tool to document my project API. I am debating whether to use Sandcastle to generate .HTML files for my Unit Tests project so someone else who reads them understands them more quickly. Is this necessary or a complete overkill? My gut is telling me – document the API with code comments, but do not document the actual test project.

The reason I ask is because I read a question on here yesterday where a user was talking about documenting the test project by explaining what each test does in more detail using Sandcastle.

Best Answer

There are 2 subtly different aspects to the documentation of unit tests.

  1. The unit test name should just as descriptive and chatty as you can make it. This is not for the purpose of documenting the test, but for providing information about a failed test at a higher level. If you see a failed test in a report, the name will tell you why the code failed, and you can go right to the code without having to check the unit test code to get the details. In other words, it documents the purpose of the test.

  2. The unit test code itself should be clear, self-documenting code, like anything else. It should only have additional comments if there is something tricky going on; and with unit tests, there should almost never be something tricky going on.

So, absolutely not. No formalized extractable comments. The use of unit tests to "document" the behavior of the code under test stems naturally from their nature, as being a collection of "example code snippets". But those "code snippets" don't require any additional comments to serve this purpose -- just read the code.

Related Topic