Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

And they are also "executable documentation". If you as a developer want to know how to use something, unit tests can be of help.

Then, writing tests for the sake of code coverage, is like judging take a driving license exam and being judged on the streets you visited or how much gas you used. Tests need to be written with logic verification in mind.

I am deeply sad this article got upvoted. People should have a sense of responsibility when giving visibility to stuff like this.



Have you seen people reading tests much in practice?

I've heard the "executable documentation" line but my general impression is that people usually read the production code. They'll only look at the tests when they fail or they need to modify them.


Sorry if this is what you were specifying when saying "executable documentation", but have you seen the way Rust handles documentation tests [1]? The idea is to provide documentation via comments before a function definition, and inside this documentation you can provide example uses. The test-runner automatically extracts these examples and verifies them. In this way, your documentation and tests are always in agreement with each other, and a developer can be aware of expected behavior by reading either the code comments or the auto-generated html docs.

I'm rather fond of this system, personally.

1: https://doc.rust-lang.org/book/testing.html#documentation-te...


Thanks, you got me testing rustlang testing facilities. Very enjoyable. Makes you want to do low level programming 10x more than with c/cpp.


That's assuming the code even makes sense. Have you ever been looking at some code, having no precise idea what it does, with no commit history to give an explanation? That's when unit tests would be useful documentation.


If I start working on a new project and they have tests, I always start there. Heck, if I resume working on an old project of mine, I start with the tests.


> I am deeply sad this article got upvoted. People should have a sense of responsibility when giving visibility to stuff like this.

Did you actually read it? Because what you just wrote, is almost exactly the same as what the article author wrote.

His point, as I read it, was that unit tests can have purpose when you don't blindly make them for code coverage.

His case for having unit tests was:

> Keep unit tests that test key algorithms for which there is a broad, formal, independent oracle of correctness, and for which there is ascribable business value

But I don't think he would disagree with the case of providing some "executable documentation" as well. A few lines of code that demonstrate how to use a class or library, along with expected results, is definitely valueable. And those are exactly the kind of interfaces you don't expect to change even when refactoring.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: