But it is different. Let me make a silly example. Let’s say I am tasked with writing a function that adds two numbers. I end up writing this:
def sum(a, 0) do
a
end
def sum(a, b) do
sum(a + 1, b - 1)
end
I then write a test for it:
test "it adds two numbers" do
assert sum(2, 3) == 5
end
I am not writing this test thinking “let’s see if my code is correct or not”. I know (or at least I am positive) that this test will pass. I write it to record in the test suite what my function should do, no matter the implementation. This test is not about correctness, it is there to prevent regressions.
A code review or quality assurance check then finds that my solution breaks if one tries to sum negative numbers. It reveals that my code is not correct, and also points out that it is cumbersome and inefficient. My test did not reveal this bug, nor the fact that the code is overly complex: does it mean it is useless?
I then discover the existence of + and change the implementation to:
def sum(a, b) do
a + b
end
I can run the previous test to make sure that it still passes. I am addressing a different concern (support for negative numbers), but I still want the previous behavior to be supported (positive numbers). The previous test is useful, precisely because I can re-run it at no cost, to make sure my new code still does well what the old code was doing successfully. I will in fact also write another test to protect from regressions regarding negative numbers:
test "it adds negative numbers" do
assert sum(2, -3) == -1
assert sum(-2, 3) == 1
assert sum(-2, -3) == -5
end






















