News for hackers. Stuff that matters.
Re:Unit Tests as Documentation by atoav on Thursday October 17, 2024 @02:30PM (#41872317)
Not sure about this, but I like it the way it is done in the Rust ecosystem.

In Rust, there are two types of comments. Regular ones (e.g. starting with //) and doc-comments (e.g. starting with ///). The latter will land in in the generated documentation when you run cargo doc.

And now the cool thing: If you have example code in these doc comments, e.g. to explain how a feature of your library can be used, that script will automatically become part of the tests per default. That means you are unlikey to forget to update these examples when your code changes and you can use them as tests at the same time by asserting something at the end (which also communicates the outcome to the reader).

Re:Unit Tests as Documentation by chrisweekly on Thursday October 17, 2024 @03:11PM (#41872805)
Does your IDE handle syntax-highlighting and intellisense -type enhancements for these unit tests written as doc-comments?
Re:Unit Tests as Documentation by bobbylarrybobby on Saturday October 19, 2024 @12:45PM (#41888867)
Yes, VSCode with rust-analyzer does
Re:Unit Tests as Documentation by chrisweekly on Thursday October 24, 2024 @11:07PM (#41941891)
Cool, thanks for the response.

I don't understand the downvote for my question (shrug).

Re:Unit Tests as Documentation by lucianbr on Thursday October 17, 2024 @03:00PM (#41872703)
Yeah, combining unit tests and written docs in various ways seems fine. My reading of the article was that the tests are the only documentation. Maybe that was not the intent but just a bad interpretation on my part.

Though some replies here seem to keep arguing for my interpretation, so it's not just me.

Re:Unit Tests as Documentation by the_af on Thursday October 17, 2024 @11:11PM (#41876048)
Combining is what TFA suggests. They even go as far as closing the article with:

> Note also that I’m not suggesting that unit tests should replace any form of documentation but rather that they should complement and enrich it.

Re:Unit Tests as Documentation by readline_prompt on Thursday October 17, 2024 @06:06PM (#41874298)
Doctests are great aren't they?
Re:Unit Tests as Documentation by TeMPOraL on Thursday October 17, 2024 @06:29PM (#41874460)
IDK, they sound like they overflow the "maximum code" counter and land up straight in the literate programming land. I wonder how far you could go writing your whole program as doctests spliced between commentary.
Re:Unit Tests as Documentation by phi-go on Friday October 18, 2024 @09:04AM (#41879065)
Doctests are tests they are not part of a release build. Also I don't believe there is a supported way to access code in doctests from the outside.

So (sadly?) no literate programming

Re:Unit Tests as Documentation by sedatk on Friday October 18, 2024 @01:39AM (#41876631)
I discovered that completely by surprise when I was learning Rust. I wrote code and unit tests, wrote some documentation, and I was blown away when I saw that my documentation also ran as part of the suite. Such a magical moment.