简体   繁体   中英

Why code coverage not 100%? pytest-cov

Why am I not getting 100% code coverage?!

All methods are tested...

How the code coverage percentage is calculated?

CODE

import os

def get_root_dir():
    return os.path.abspath(os.path.join(os.path.sep, os.path.dirname(os.path.realpath(__file__)), '../../'))


def get_coverage_report_dir():
    return os.path.join(os.path.sep, get_root_dir(), 'coverage_report')

TEST

import unittest

class TestPaths(unittest.TestCase):

def test_paths(self):

    import src.utils.paths as paths
    self.assertTrue(paths.get_root_dir().endswith('myproject'))
    self.assertTrue(paths.get_root_dir() in paths.get_coverage_report_dir() and paths.get_coverage_report_dir().endswith('coverage_report'))

REPORT

---------- coverage: platform win32, python 2.7.14-final-0 -----------
Name                               Stmts   Miss  Cover
------------------------------------------------------
src\utils\__init__.py                  0      0   100%
src\utils\example_util_module.py       2      0   100%
src\utils\paths.py                     5      3    40%
------------------------------------------------------
TOTAL                                  7      3    57%

The html output will show you the missing lines

pytest --cov=paths --cov-report=html

Then open index.html from the newly created htmlcov folder.

Here is a video demonstrating achieving 100 percent test coverage.

https://youtu.be/7BJ_BKeeJyM

Python 100%的代码覆盖率

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM