Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should unit tests be documented? [closed]

Tags:

I'm trying to improve the number and quality of tests in my Python projects. One of the the difficulties I've encountered as the number of tests increase is knowing what each test does and how it's supposed to help spot problems. I know that part of keeping track of tests is better unit test names (which has been addressed elsewhere), but I'm also interested in understanding how documentation and unit testing go together.

How can unit tests be documented to improve their utility when those tests fail in the future? Specifically, what makes a good unit test docstring?

I'd appreciate both descriptive answers and examples of unit tests with excellent documentation. Though I'm working exclusively with Python, I'm open to practices from other languages.

like image 840
ddbeck Avatar asked Nov 13 '09 01:11

ddbeck


People also ask

Should unit tests be documented?

Unit tests should aim to be self descriptive, but there will always be cases where this goal cannot be achieved and thus description of what has been written is due. In other words, try to make your unit tests so they don't need documentation, but if they need it, write it!

What should I cover unit test with?

The purpose of a unit test in software engineering is to verify the behavior of a relatively small piece of software, independently from other parts. Unit tests are narrow in scope, and allow us to cover all cases, ensuring that every single part works correctly.

Should unit tests be self contained?

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


1 Answers

I document most on my unit tests with the method name exclusively:

testInitializeSetsUpChessBoardCorrectly() testSuccessfulPromotionAddsCorrectPiece() 

For almost 100% of my test cases, this clearly explains what the unit test is validating and that's all I use. However, in a few of the more complicated test cases, I'll add a few comments throughout the method to explain what several lines are doing.

I've seen a tool before (I believe it was for Ruby) that would generate documentation files by parsing the names of all the test cases in a project, but I don't recall the name. If you had test cases for a chess Queen class:

testCanMoveStraightUpWhenNotBlocked() testCanMoveStraightLeftWhenNotBlocked() 

the tool would generate an HTML doc with contents something like this:

Queen requirements:  - can move straight up when not blocked.  - can move straight left when not blocked. 
like image 193
Kaleb Brasee Avatar answered Oct 20 '22 14:10

Kaleb Brasee