如何解决为什么覆盖率报告因我的执行方式而异
我正在编写一个 django 应用程序。我正在尝试覆盖范围的单元测试和 pytest。根据我运行测试的方式,我会得到不同的覆盖率报告。
相关代码
我在 settings_test.py 中有这个,pytest 的一个运行器:
class PytestTestRunner(UnManagedModelTestRunner):
def __init__(self,verbosity=1,failfast=False,keepdb=False,**kwargs):
self.verbosity = verbosity
self.failfast = failfast
self.keepdb = keepdb
def run_tests(self,test_labels):
"""Run pytest and return the exitcode.
It translates some of Django's test command option to pytest's.
"""
import pytest
argv = []
if self.verbosity == 0:
argv.append('--quiet')
if self.verbosity == 2:
argv.append('--verbose')
if self.verbosity == 3:
argv.append('-vv')
if self.failfast:
argv.append('--exitfirst')
if self.keepdb:
argv.append('--reuse-db')
argv.extend(test_labels)
return pytest.main(argv)
TEST_RUNNER = "settings_test.UnManagedModelTestRunner"
还有这个脚本来运行测试:
runner.py
USAGE = """
USAGE:
python runner.py (will run unittest suite)
python runner.py unittest (will run unittest suite)
python runner.py pytest (will run pytest suite + coverage)
"""
TEST_RUNNERS_MAP = {
'pytest': 'testapp.settings_test.PytestTestRunner',# pytest
'unittest': 'testapp.settings_test.UnManagedModelTestRunner',# unittest
}
if __name__ == "__main__":
os.environ['DJANGO_SETTINGS_MODULE'] = 'testapp.settings_test'
django.setup()
# Get argument to decide which test suite to run.
arguments = sys.argv[1:]
arg = arguments[0] if 0 < len(arguments) else "unittest"
runner = TEST_RUNNERS_MAP[arg]
TestRunner = get_runner(settings,test_runner_class=runner)
test_runner = TestRunner()
failures = test_runner.run_tests(["unit_tests"])
sys.exit(bool(failures))
测试运行
$ pytest
Name Stmts Miss Branch BrPart Cover Missing
------------------------------------------------------------------------------------------------
api/apps.py 4 0 0 0 100%
api/models/enum_model_base.py 25 10 2 0 56% 14,28,31
api/models/ingredient.py 11 0 0 0 100%
api/models/model_base_with_timestamp.py 6 0 0 0 100%
api/routes.py 5 0 0 0 100%
api/serializers/ingredients.py 6 6 0 0 0% 1-9
api/urls.py 5 5 0 0 0% 1-7
api/views/view_set_base.py 55 55 10 0 0% 1-123
api/views/ingredients.py 6 6 0 0 0% 1-9
------------------------------------------------------------------------------------------------
TOTAL 123 82 12 0 30%
FAIL required test coverage of 100.0% not reached. Total coverage: 30.37%
$ coverage run runner.py unittest
$ coverage report
Name Stmts Miss Branch BrPart Cover Missing
------------------------------------------------------------------------------------------------
api/apps.py 4 0 0 0 100%
api/models/enum_model_base.py 25 10 2 0 56% 14,31
api/models/ingredient.py 11 0 0 0 100%
api/models/model_base_with_timestamp.py 6 0 0 0 100%
api/routes.py 5 0 0 0 100%
api/serializers/ingredients.py 6 6 0 0 0% 1-9
api/urls.py 5 0 0 0 100%
api/views/view_set_base.py 55 55 10 0 0% 1-123
api/views/ingredients.py 6 6 0 0 0% 1-9
但是如果我从脚本运行 pytest。大多数都是0%
$ python runner.py pytest
Name Stmts Miss Branch BrPart Cover Missing
------------------------------------------------------------------------------------------------
api/apps.py 4 4 0 0 0% 1-6
api/models/enum_model_base.py 25 25 2 0 0% 1-57
api/models/ingredient.py 11 11 0 0 0% 1-14
api/models/model_base_with_timestamp.py 6 6 0 0 0% 1-13
api/routes.py 5 0 0 0 100%
api/serializers/ingredients.py 6 6 0 0 0% 1-9
api/urls.py 5 5 0 0 0% 1-7
api/views/view_set_base.py 55 55 10 0 0% 1-123
api/views/ingredients.py 6 6 0 0 0% 1-9
------------------------------------------------------------------------------------------------
TOTAL 123 118 12 0 4%
FAIL required test coverage of 100.0% not reached. Total coverage: 3.70%
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。