Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 当使用太少的参数进行调用时,Autospec未失败_Python 2.7_Python Unittest_Python Unittest.mock - Fatal编程技术网

Python 2.7 当使用太少的参数进行调用时,Autospec未失败

Python 2.7 当使用太少的参数进行调用时,Autospec未失败,python-2.7,python-unittest,python-unittest.mock,Python 2.7,Python Unittest,Python Unittest.mock,我正在尝试找出在测试中使用autospec的最佳方法。在下面的场景中,我使用autospec来检测何时使用过多参数进行调用。当使用$python filename.py调用此场景时,其效果与预期一样 import unittest import mock class FakeDependency(object): def set_obj(arg1, arg2): self.foo = arg1 self.bar = arg2 class FakeOb

我正在尝试找出在测试中使用autospec的最佳方法。在下面的场景中,我使用autospec来检测何时使用过多参数进行调用。当使用
$python filename.py
调用此场景时,其效果与预期一样

import unittest
import mock

class FakeDependency(object):
    def set_obj(arg1, arg2):
        self.foo = arg1
        self.bar = arg2


class FakeObject(object):
    def __init__(self):
        self.fake_dependency = FakeDependency()

    def set_dependency(self):
        self.fake_dependency.set_obj('foo', 'bar', 'buz')  # Bug


class TestFakeObject(unittest.TestCase):

    @mock.patch.object(FakeDependency, 'set_obj')
    def test_set_dependency_with_assert_called(self, mock_dependency):
        r = FakeObject()
        r.set_dependency()
        mock_dependency.assert_called_once_with('foo', 'bar', 'buz')  # Test passes does not catch bug


    @mock.patch.object(FakeDependency, 'set_obj', autospec=True)
    def test_set_dependency_with_autospec(self, mock_dependency):
        r = FakeObject()
        r.set_dependency()  # Test Fails


if __name__ == '__main__':
    unittest.main()
但是如果我改变测试用例来检测何时调用的参数太少,那么它就会通过并且不会捕获错误

class FakeDependency(object):
    def set_obj(arg1, arg2, arg3):
        self.foo = arg1
        self.bar = arg2
        self.buz = arg3


class FakeObject(object):
    def __init__(self):
        self.fake_dependency = FakeDependency()

    def set_dependency(self):
        self.fake_dependency.set_obj('foo', 'bar')  # Bug


class TestFakeObject(unittest.TestCase):

    @mock.patch.object(FakeDependency, 'set_obj')
    def test_set_dependency_with_assert_called(self, mock_dependency):
        r = FakeObject()
        r.set_dependency()
        mock_dependency.assert_called_once_with('foo', 'bar')  # Test passes does not catch bug


    @mock.patch.object(FakeDependency, 'set_obj', autospec=True)
    def test_set_dependency_with_autospec(self, mock_dependency):
        r = FakeObject()
        r.set_dependency()  # Test Also passes

所以我的问题是我做错了什么?根据autospec,当通过的参数太少时,autospec也应该检测。

正如sxn指出的,我认为测试是错误的。当我这样调用测试时,它按预期工作

$ python -m unittest filename

正如sxn指出的,我认为测试是错误的。当我这样调用测试时,它按预期工作

$ python -m unittest filename

你确定你正确运行了测试吗?我将您的第一个代码片段中的代码复制并粘贴到
main.py
文件中,安装mock并使用
python-m unittest main
运行它,得到
TypeError:位置参数太多,这是您所期望的。:)

您确定测试运行正确吗?我将您的第一个代码片段中的代码复制并粘贴到
main.py
文件中,安装mock并使用
python-m unittest main
运行它,得到
TypeError:位置参数太多,这是您所期望的。:)

我只是使用python文件名运行它。py@sxn这就是我的问题,当我更改对python-m unittest文件名的调用时,它按预期工作。py@sxn这就是我的问题,当我更改对python-m unittest文件名的调用时,它按预期工作。酷!很高兴它有帮助:)酷!很高兴这有帮助:)