Search code examples
pythonnosecoverage.py

python coverage missing tests


I'm running python coverage but my coverage is not 100%.

def add(a, b):
    return a + b

test_utils.py

from python_coverage import utils
import unittest


class TestAdd(unittest.TestCase):
    """
    Test the add function from the mymath library
    """

    def test_add_integers(self):
        """
        Test that the addition of two integers returns the correct total
        """
        result = utils.add(1, 2)
        self.assertEqual(result, 3)

    def test_add_floats(self):
        """
        Test that the addition of two floats returns the correct result
        """
        result = utils.add(10.5, 2)
        self.assertEqual(result, 12.5)

    def test_add_strings(self):
        """
        Test the addition of two strings returns the two string as one
        concatenated string
        """
        result = utils.add('abc', 'def')
        self.assertEqual(result, 'abcdef')

if __name__ == '__main__':
    unittest.main()        

coverage report -m

Name                          Stmts   Miss  Cover   Missing
-----------------------------------------------------------
python_coverage/__init__.py       0      0   100%
python_coverage/utils.py          2      1    50%   2
tests/test_utils.py              12      6    50%   7-8, 11-12, 15-16
-----------------------------------------------------------
TOTAL                            14      7    50%

I copied the code from that link and run the report and here is it -

Name                          Stmts   Miss  Cover   Missing
-----------------------------------------------------------
python_coverage/__init__.py       0      0   100%
python_coverage/utils.py          2      1    50%   2
tests/test_utils.py              14      8    43%   14-15, 21-22, 29-33
-----------------------------------------------------------
TOTAL                            16      9    44%

Solution

  • You are not testing the output of methods. This test:

    r = utils.add(2,3)
    self.assertEqual(5,5)
    

    does not actually test the value of r. Try changing this to:

    r = utils.add(2, 3)
    self.assertEqual(r, 5)
    

    The same with TestAdd.test_add_float.