Unit testing, science and math


Foreword


Unit testing is commonly used. It seems that no one can do without him, everyone writes tests, and their absence in any serious project causes, at least, misunderstanding. However, many perceive testing as a ritual performed in order not to anger the “programming god”. Like, it’s necessary . Why? Because .


I’ll say scary things.


It doesn’t matter what to take as a testing unit. It doesn't matter how the tests are grouped. It doesn’t matter whether they are written before or after the code. TDD or not TDD? Does not matter. Share of coverage? Don't give a damn. After all, there may not be any tests at all. All this is completely irrelevant. It is important that the software requirements are met.


Unit tests are not a ritual, but a good, working tool, allowing you to get closer to meeting these requirements. And this tool needs to be able to use correctly.


Wait, where does science with math come in?


Content



  1. Warranties and Probabilities
  2. Programming as a theory
  3. Testing as a proof of theorems
  4. What is important and what is not
  5. Do I need to test tests


Warranties and Probabilities


I will not discuss for the hundredth time why tests are needed, how much they help, document, simplify the development and refactoring process, close regressions, etc.


I want to write about how to make out in tests something more than a series of obligations, technical nuances and management requirements. So that the tests are not props that save shaky structures from immediate destruction, but the foundation on which our programs stand firmly and confidently.


, — . — . — . , , , , .


. . : , , 100%, .


.




"". , , , , . , , . "" "". , . , , "" , , . , "", , , .


, , , . , — , .


, , .




. "", "". "" "" "".


"" , , . "" — , , ( , ).


"" , , , , — . "" "" , , "".


"" , "" "", "" "", .


. , std::vector, clear. , "", .

? , (capacity). , "":
  1. clear() , size() ;
  2. clear() , empty() true;
  3. clear() , capacity() , clear().
  4. clear() , , , .


"" "":
test_case("   `clear()`    ")
{
    std::vector<int> v{1, 2, 3, 4};

    v.clear();

    check(v.size() == 0);
}


, "", ? .

, , . — , "". , "" "" , size(). "".

, "", std::vector<int> v{1, 2, 3, 4}. . , "", ? , , . ? , , ( , ).

"" , , : "", , , . , "".

"", size() , . : , . "", .

"" "".

, "", ( , ) .



,


, , , (, , - — — ). , "" "". — .


, , : , ( TDD ). — "". , , "", TDD, .


, . , , . , , .


, , , .




— , . , , . , . , .


Thus, our metaphorical "science", that is, code, and "objective reality", that is, tests, move towards each other, never reaching the ideal, but increasing the desired probability.


All Articles