gpt4 book ai didi

python - 使用覆盖率,我如何测试这条线?

转载 作者:太空狗 更新时间:2023-10-29 17:07:02 26 4
gpt4 key购买 nike

我有一个简单的测试:

class ModelTests(TestCase):

def test_method(self):
instance = Activity(title="Test")
self.assertEqual(instance.get_approved_member_count(), 0)

我的问题是覆盖率仍然显示未测试的 get_approved_member_count 行:

enter image description here

如何满足上述覆盖范围?

为了运行测试,我使用了 Django Nose with Coverage:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = [
'--with-coverage',
'--cover-html',
'--cover-package=apps.users,apps.activities',
]

控制台:

python manage.py test
/Users/user/Documents/workspace/api/env/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes.
return f(*args, **kwds)

/Users/user/Documents/workspace/api/env/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: The utilities in django.db.models.loading are deprecated in favor of the new application loading system.
return f(*args, **kwds)

nosetests --with-coverage --cover-html --cover-package=apps.users,apps.activities --verbosity=1




Name Stmts Miss Cover Missing
---------------------------------------------------------------------------------------
apps.activities 0 0 100%
apps.activities.admin 8 8 0% 1-14
activities.migrations 0 0 100%
activities.migrations.0001_initial 9 0 100%
apps.activities.urls 8 0 100%


etc etc etc
---------------------------------------------------------------------------------------
TOTAL 670 232 65%
----------------------------------------------------------------------
Ran 79 tests in 17.101s

最佳答案

覆盖率报告显示正在调用该方法(第 80 行为绿色)。但它也表明它从未被定义(第 75 行是红色的)。

这是一个典型的覆盖开始太晚的问题。解决此问题的最简单方法是使用覆盖率来运行测试运行器,而不是使用测试运行器来运行覆盖率:

$ coverage run -m nose --verbosity=1

更新:与您的原始命令一起使用:

$ coverage run manage.py test

但您需要先卸载 Nose 覆盖插件。

关于python - 使用覆盖率,我如何测试这条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30582815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com