Aspects of good unit tests

This article is a synopsis of the book " Unit Testing Principles ".





Let's start by listing the properties of good unit tests.





First. Integrated into the development cycle. Only those tests that you actively use are useful; otherwise it makes no sense to write them.





Second. Only the most important parts of your code are tested. Not all working code deserves the same attention.





Third. Provide maximum protection against bugs with minimum maintenance costs. To do this, you need to be able to recognize effective tests and write them.





However, recognizing and writing an effective test are two different skills. And to acquire the second skill, you must first master the first. The rest of this article will show you how to recognize an effective test. The pyramid of testing and testing according to the "black box" / "white box" principle will also be considered.





Four Aspects of Good Unit Tests

A good unit test should have the following attributes: bug protection, refactoring resistance, fast feedback, and ease of maintenance.





These four attributes are fundamental. They can be used to analyze any automated tests, be they unit tests, integration tests, or end-to-end tests.





Let's start with the first attribute of a good unit test: bug protection. A bug (or regression) is a software bug. As a rule, such errors occur after making changes to the code.





The more functionality, the higher the likelihood that you will add a bug to the new version. This is why it is so important to develop good bug protection. Without such protection, it will be impossible or very difficult to ensure the growth of the project in the long term due to the constantly increasing number of errors.





To assess how well a test performs in terms of bug protection, consider the following:





  • the amount of code executed by the test;





  • the complexity of this code;





  • the importance of this code in terms of business logic.





, ,  . ,   (assertions).





, -. , -, โ€”  .





, .   - -.





- โ€” . ,   .





.   : . โ€”   .





. :  , , . , , . , .





?   .  - .   ,     .





:





  • ,   .   .   .





  •   . -,   . , .





?

, , .  , .  :  . โ€” ,  , .





โ€” , . , ,   . โ€” ,   .





Fig.  1 - The test on the left is related to the observed behavior of the SUT, not the implementation details.  Such a test is more resistant to refactoring than the test on the right.
. 1 โ€“ SUT, . ,

- ( ) .   , . - :   ,   .





, .   ( ),   , ( ).





, , , :   ( ).





Fig.  2 - Relationship between bug protection and refactoring resistance
. 2 -

, , .   : . .     โ€” II .





, :  , . . .





: , . :





  • ( , );





  • ( , ).





. 3,   . โ€” (),   . โ€”  (), .





Fig.  3 - Formula of test accuracy
. 3 โ€“

:  

  -. ,   .  . , ,   ,  .





. :





  • . .   , .





  • . , , .





.   ยซยป :   , .





  , .   , .





? , . , โ€”  , โ€”  . :   .





, . ,   . ,  , .





โ€” (end-to-end) . .  ,  , .





, . , , ,  .  - , .





: . ,   , . .





โ€”  . ,  .





Fig.  4 - Trivial test covering a simple piece of code
. 4 - ,

, . , ,   .   - ,   .





-  , , . :   , .





Fig.  5 - Places that the tests take in relation to each other
. 5 โ€“ ,

โ€” โ€” , (end-to-end) . - ,   .   . ,  .





.   ; . , .  , ,   . .





,   ; .   .





Fig.  6 - Tradeoffs between attributes of a good test
. 6 โ€“

?  :   , .  . ,   . ,   .





- CAP. ,   : (consistency) , (availability), (partition tolerance).





:





1. CAP ;





2.   . โ€”  , , - Amazon โ€” .     .





  : -, , .





.   . , . , .   .   ,   .





Fig.  7 - The testing pyramid prescribes a certain ratio of unit, integration and end-to-end tests
. 7 - -,
Fig.  8 - Different types of tests in the pyramid make different decisions regarding fast feedback and protection from bugs
. 8 -

. :   ; - โ€” ;   - .





, โ€“  . :   . ,   .





. - ,   -, โ€” .   ;   , ,  (, ). CRUD-  - .





โ€” API, (, ).   . , .   ,   โ€” .





ยซ ยป ยซ ยป

ยซ ยป .   . , , , .





ยซ ยป  . . ,   .





Fig.  9 - Advantages and disadvantages of black-box and white-box testing
. 9 - ยซ ยป ยซ ยป

,   : , .   ยซ ยป. โ€” , -, โ€” ยซ ยป  , -.   -, .





  • -





  • Aspects of good unit tests





  • What is integration testing for?








All Articles