使用覆盖,我该如何测试这行? [英] Using coverage, how do I test this line?

查看:134
本文介绍了使用覆盖,我该如何测试这行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测试:

class ModelTests(TestCase):

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

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

My problem is that coverage still shows get_approved_member_count line as NOT tested:

我满足以上的覆盖面?

How do I satisfy the above for coverage?

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

To run the tests I'm using 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行是红色的)。

The coverage report shows that the method is being called (line 80 is green). But it also shows that it was never defined (line 75 is red).

这是一个经常出现覆盖率太晚的问题。解决这个问题的最简单的方法是使用覆盖来运行测试运行器,而不是使用测试运行器来运行覆盖:

This is a classic problem of starting coverage too late. The simplest way to fix this is to use coverage to run your test runner, instead of using the test runner to run coverage:

$ coverage run -m nose --verbosity=1

UPDATED:用于您的原始命令: / p>

UPDATED: to use with your original command:

$ coverage run manage.py test

,但您首先要卸载鼻子覆盖插件。

but you'd want to uninstall the nose coverage plugin first.

这篇关于使用覆盖,我该如何测试这行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆