in generalwe should be quite stingy with mocks if we find ourselves mocking out multiple elements in given unit testwe may end up testing the mock framework rather than our real code this serves no useful purpose whatsoeverafter allmocks are well-tested alreadyif our code is doing lot of thisit' probably another sign that the api we are testing is poorly designed mocks should exist at the boundaries between the code under test and the libraries they interface with if this isn' happeningwe may need to change the api so that the boundaries are redrawn in different place how much testing is enoughwe've already established that untested code is broken code but how can we tell how well our code is testedhow do we know how much of our code is actually being tested and how much is brokenthe first question is the more important onebut it' hard to answer even if we know we have tested every line of code in our applicationwe do not know that we have tested it properly for exampleif we write stats test that only checks what happens when we provide list of integersit may still fail spectacularly if used on list of floats or strings or self-made objects the onus of designing complete test suites still lies with the programmer the second question--how much of our code is actually being tested--is easy to verify code coverage is essentially an estimate of the number of lines of code that are executed by program if we know that number and the number of lines that are in the programwe can get an estimate of what percentage of the code was really testedor covered if we additionally have an indicator as to which lines were not testedwe can more easily write new tests to ensure those lines are less broken the most popular tool for testing code coverage is calledmemorably enoughcoverage py it can be installed like most other third-party libraries using the command pip install coverage we don' have space to cover all the details of the coverage apiso we'll just look at few typical examples if we have python script that runs all our unit tests for us (for exampleusing unittest maina custom test runner or discover)we can use the following command to perform coverage analysiscoverage run coverage_unittest py this command will exit normallybut it creates file named coverage that holds the data from the run we can now use the coverage report command to get an analysis of code coveragecoverage report