首页 > 解决方案 > 测试目录中 python3 Flask、unittest 和 cntext.py 中的覆盖率报告未涵盖的行

问题描述

当我在运行我测试过的方法中的任何行coverage report -m之后运行命令时,我使用一个文件并且我有以下项目结构coverage run main.pycontext.py

my_project
├── service
│   ├── __init__.py        
│   └── setThings.py
└── test
    ├── context.py 
    ├── __init__.py         
    └── test_setThings.py

这是我的 context.py 的内容

import sys
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import service
from service import setThings

这是我的服务 setThings.py

from jinja2 import Environment, FileSystemLoader

def check_if_json_property_is_null(json_property):
    if(json_property != ""):
        return json_property
    else:
        return "&nbsp"

这是我的测试文件

import unittest
import json
from collections import namedtuple

from .context import setThings

class Test_SetThings(unittest.TestCase):

    def test_given_none_property_when_checking_if_none_return_empty(self):
        #ASSERT
        self.assertEqual("&nbsp", setThings.check_if_json_property_is_null(""))

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

所以在我运行coverage report -m它之后说我的服务方法中的行没有被覆盖,我在我的需求文件中使用了coverage==4.5.3,

另外我正在从项目根目录运行所有命令

标签: pythonpython-unittestcoverage.py

解决方案


推荐阅读