python 中 super() 的单元测试用例
unit test case for super() in python
我有一个 class 作为:
"""A"""
from clay import config
from datetime import timedelta
from <path> import B
EXECUTION_TIMEOUT = timedelta(minutes=30)
class A(B):
"""Crawler task for A"""
def __init__(self, *args, **kwargs):
"""Initialization."""
super(A, self).__init__(execution_timeout=EXECUTION_TIMEOUT, *args, **kwargs)
我有一个测试文件:
"""Test class for A"""
from datetime import timedelta, datetime
from doubles import allow
from mock import patch, MagicMock
from unittest import TestCase
import mock
from <path> import A
class JobInfoHandler:
"""Mock job info."""
def __init__(self):
"""Init Method."""
self.crawlerConfig = None
pass
class TestA(TestCase):
"""Test class for A."""
def setUp(self):
"""Create instance of class to test."""
job_info_ob = JobInfoHandler()
self.obj_a = A(owner='arahej', task_id='12',
schedule_interval=timedelta(minutes=60), job_info=job_info_ob)
当我 运行 我的测试覆盖率命令。下面一行不包括在内:
super(A, self).__init__(execution_timeout=EXECUTION_TIMEOUT, *args, **kwargs)
任何人都可以帮助我如何覆盖 super
这里。我尝试模拟或修补,但没有用。
尝试修补超级:
from mock import patch
class B(object):
def foo(self):
print('foo from B')
class A(B):
def foo(self):
self.called_foo = True
super(A, self).foo()
class TestA(object):
@patch('%s.super' % __name__, create=True)
def test_foo(self, super_func):
A = A()
A.foo()
assert A.called_foo
assert super_func.called
setUp()
方法只在你写一个测试方法时调用documented:
setUp()
Method called to prepare the test fixture. This is called immediately before calling the test method;...
class TestA(TestCase):
"""Test class for A."""
def setUp(self):
"""Create instance of class to test."""
print("Setting up testcase")
job_info_ob = JobInfoHandler()
self.obj_a = A(owner='arahej', task_id='12',
schedule_interval=timedelta(minutes=60), job_info=job_info_ob)
这里,你还没有定义任何测试方法,因此 setUp()
永远不会被调用,因此 A
永远不会被初始化,这反过来也永远不会调用父 [=18] 的初始化=] 通过 super().__init__()
.
$ coverage run -m unittest discover
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
$ coverage html
添加测试方法现在 运行 setUp()
将初始化 A.__init__()
,然后 运行s B.__init__()
通过 super()
。
class TestA(TestCase):
def setUp(self):
...
def test_first(self):
print("1st test method called")
assert True
$ coverage run -m unittest discover
Setting up testcase
1st test method called
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$ coverage html
此外,您提到了 mocks/patches。您可能误解了 mocks/patches 的用途,因为如果您 mock/patch 使用存根实现调用 super().__init__()
,那么它不会调用实际实现,而是调用 mocked/patched/stubbed版本,这意味着它不会解决您的问题,因为当您用修补的调用替换实际调用时,您的实际实现的覆盖范围仍然会丢失。换句话说,你只是通过修补让你的问题变得更糟:)
我有一个 class 作为:
"""A"""
from clay import config
from datetime import timedelta
from <path> import B
EXECUTION_TIMEOUT = timedelta(minutes=30)
class A(B):
"""Crawler task for A"""
def __init__(self, *args, **kwargs):
"""Initialization."""
super(A, self).__init__(execution_timeout=EXECUTION_TIMEOUT, *args, **kwargs)
我有一个测试文件:
"""Test class for A"""
from datetime import timedelta, datetime
from doubles import allow
from mock import patch, MagicMock
from unittest import TestCase
import mock
from <path> import A
class JobInfoHandler:
"""Mock job info."""
def __init__(self):
"""Init Method."""
self.crawlerConfig = None
pass
class TestA(TestCase):
"""Test class for A."""
def setUp(self):
"""Create instance of class to test."""
job_info_ob = JobInfoHandler()
self.obj_a = A(owner='arahej', task_id='12',
schedule_interval=timedelta(minutes=60), job_info=job_info_ob)
当我 运行 我的测试覆盖率命令。下面一行不包括在内:
super(A, self).__init__(execution_timeout=EXECUTION_TIMEOUT, *args, **kwargs)
任何人都可以帮助我如何覆盖 super
这里。我尝试模拟或修补,但没有用。
尝试修补超级:
from mock import patch
class B(object):
def foo(self):
print('foo from B')
class A(B):
def foo(self):
self.called_foo = True
super(A, self).foo()
class TestA(object):
@patch('%s.super' % __name__, create=True)
def test_foo(self, super_func):
A = A()
A.foo()
assert A.called_foo
assert super_func.called
setUp()
方法只在你写一个测试方法时调用documented:
setUp()
Method called to prepare the test fixture. This is called immediately before calling the test method;...
class TestA(TestCase):
"""Test class for A."""
def setUp(self):
"""Create instance of class to test."""
print("Setting up testcase")
job_info_ob = JobInfoHandler()
self.obj_a = A(owner='arahej', task_id='12',
schedule_interval=timedelta(minutes=60), job_info=job_info_ob)
这里,你还没有定义任何测试方法,因此 setUp()
永远不会被调用,因此 A
永远不会被初始化,这反过来也永远不会调用父 [=18] 的初始化=] 通过 super().__init__()
.
$ coverage run -m unittest discover
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
$ coverage html
添加测试方法现在 运行 setUp()
将初始化 A.__init__()
,然后 运行s B.__init__()
通过 super()
。
class TestA(TestCase):
def setUp(self):
...
def test_first(self):
print("1st test method called")
assert True
$ coverage run -m unittest discover
Setting up testcase
1st test method called
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$ coverage html
此外,您提到了 mocks/patches。您可能误解了 mocks/patches 的用途,因为如果您 mock/patch 使用存根实现调用 super().__init__()
,那么它不会调用实际实现,而是调用 mocked/patched/stubbed版本,这意味着它不会解决您的问题,因为当您用修补的调用替换实际调用时,您的实际实现的覆盖范围仍然会丢失。换句话说,你只是通过修补让你的问题变得更糟:)