Search code examples
tdd

Why should I use triangulation instead of just randomized values in my tests?


Since I started learning and using TDD I was wondering something about the "triangulation technique": why bother writing multiple tests when we could write one that use randomized values?

Today, I stumbled upon this article, which explains the "triangulation technique". Then I posted a question to the author about this "randomization over triangulation" questionment. I am very interested in his view on this, but I was also wanting to gather as many point of views as possible on this matter.

What he explains is very interesting, because I prefered randomized values as I thought that using one or two or even three differents values do not "prove" that my system is robust. But his insight on the refactoring phase that should also avoid duplication between the test and the implementation is the key.

That being said, I still find it a bit useless. I mean: using this technique, one has to write multiple similar tests (at least 2), an implementation that will evolve, and then do a refactoring to all of this to remove all tests but one. The remaining test will not express the complexity of the final solution, as there is no direct link between it and the implementation. It does not endorse its role of "documenting" the implementation. It cannot be used to lead to the implementation once again.

With a randomized value, on the other hand, I directly have to adjust my implementation to the fact that the value is not known. I cannot use a simple constant to make the test pass. I do not have to write multiple tests that will be removed. I do not have to be rigorous enough to not forget to do this test-implementation de-duplication refactoring. And my test do express the correct need served by the implementation (i.e.: given "any" two numbers, I return the sum).

So, over your experience, do you see any advantage of triangulation over the use of randomized values?


Solution

  • When Kent Beck first introduced the triangulation metaphor, he wrote:

    I've tried Triangulation, but the steps don't seem valuable to me, so I quickly lose motivation.


    So, over your experience, do you see any advantage of triangulation over the use of randomized values?

    This is a somewhat complicated question to unpack.

    There are a few demonstrations of TDD with property based tests on line.

    When I try it, however, I quickly run into a couple problems

    1. Unless you're trying to solve a trivial problem, defining properties is hard.

    2. It's not at all clear that tests are the right tool to use to ensure that properties hold. For instance, Jim Coplien wrote that most unit tests should be assertions.

    Part of the tension here is that, while property driven tests may be better for testing, the intention of TDD is in the realm of analysis and design.

    I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence -- Beck, 2008

    The checks that we create in TDD are by the developers for the developers. So unless testing with random data provides design insights that aren't otherwise available, it seems to be a priority of wearing the "wrong" hat.