Hello I will try to explain what is my actual understanding of tests and why I have problems to grasp it's utility.
Even if I try my hardest to understand the concept of TTD, unit test, integration test, etc. From the most articles that I read it explain HOW to do it (with a trivial example or a framework) and sometimes how the tests saved them hours of debug in certain situation. And I have two problems with that :
First : the method
Let's use a basic example for the explaination of how to do a unit test :
You want to test that your add function works as expected so in your test file you write a mock of your function with an expected output lets say something like that :
import add from ./calculate function myTestingFunction() { add(3, 5) expectedOutput(8) }
Okay so you explain me how to do it but not why... and even with that, I have multiple questions :
- You're testing a function with another function ... so why did you stop there and test the function you just created ?
- It's code, it has algorithm so how to be sure it is correctly written ?
- And why stop with the expected output ? Why not testing that the input are of the correct type, because if I give characters to my add function it will fail, no ?
And every article is basically like that. So with that questions in mind I cannot find the motivation to even try this methods : it's just don't seems logic.
Second : Tests are time and money saving
Let's say now I want to test the Divide function the same way and write and expected output of 3 for the input 9 and 3.
Okay so I write the same function than explained before. But if the user tries to divide by 0 a serious bug will logically occur for him ... this test doesn't covers this case and I'm writing test to ensure my code works well (or at least give the correct outputs).
In real life I now see two situation :
- I had explained to my client that I will take more time to code (so he will pay me more) but as a result he will be sure that in final his functions are bullet proof. And here I'm saying to him :
"Well ... sorry I didn't think about this use case"
"So I pay you more because you said that the tests you've written will ensure a bug free app... but it's not ? And now I have to pay you to debug it ?"
(so no time and money save and a bad relation with my client)
- I had coded the functionnality as explained and understood by me and my client. The new use case occur that we were'nt aware of. 'Kay no problem : I pull a branch to bug fix, fix the bug, push the hotfix, document the special use case if needed. And now in the future if I encounter an equivalent problematic I will have the algorithm already prepared because I faced the issue (time and money save for me and my client and a better relation overall)
I don't know if I'm clear about my state of mind but I've discussed these elements with many of my colleagues (some even with more years of practice or a better school degree than me) and they had the same problem than me about test : even if we really want to understand this topic and implement a good way of working if needed, as of now i'ts seems like some sort of intelectual pride.
UPDATE
Thank you all for your answers and comments, I learned a lot of terminology that will help my future research in order to clear my mind.
I also want to mark a new point :
How to make a relevant test
It seems you assume I know how to make a good test set to prove a point of my code or to validate my understanding of a function before pushing it to prod or anything.
It's not the case : even if I know how to do it regarding syntax to use or framework to implement let's say. I didn't find any resources to learn what is considered a "good" test set or a "right" way of doing it. That's why I don't know where do I stop the scenario of testing or how many expecting outputs it's need, if it is a question of quantity of output as well or if can just narrow to the not expected behavior maybe :
unexpectedOutput(!= 8) if my output differ from 8 my test fail
I didn't find resources to this very root knowledge about test and it seems to be a complete different job than the web developper that I am.
The same way I can configure let's say a CI/CD pipeline and implement several tools around it to automate it. But I'm not a devops, there are surely errors of optimisations I made or another tool more dedicated to the one I used but I didn't learn about it, etc.