当前位置: 首页>>代码示例>>Python>>正文


Python Mock.assert_called_with方法代码示例

本文整理汇总了Python中mock.Mock.assert_called_with方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.assert_called_with方法的具体用法?Python Mock.assert_called_with怎么用?Python Mock.assert_called_with使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mock.Mock的用法示例。


在下文中一共展示了Mock.assert_called_with方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_with_defaults

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
 def test_with_defaults(self, objective, get_output):
     loss_function, target = Mock(), Mock()
     loss_function.return_value = np.array([1, 2, 3])
     result = objective([1, 2, 3], loss_function=loss_function, target=target)
     assert result == 2.0
     get_output.assert_called_with(3, deterministic=False)
     loss_function.assert_called_with(get_output.return_value, target)
开发者ID:buyijie,项目名称:nolearn,代码行数:9,代码来源:test_base.py

示例2: testSideEffect

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def testSideEffect(self):
        mock = Mock()

        def effect(*args, **kwargs):
            raise SystemError('kablooie')

        mock.side_effect = effect
        self.assertRaises(SystemError, mock, 1, 2, fish=3)
        mock.assert_called_with(1, 2, fish=3)

        results = [1, 2, 3]
        def effect():
            return results.pop()
        mock.side_effect = effect

        self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
                          "side effect not used correctly")

        mock = Mock(side_effect=sentinel.SideEffect)
        self.assertEqual(mock.side_effect, sentinel.SideEffect,
                          "side effect in constructor not used")

        def side_effect():
            return DEFAULT
        mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN)
        self.assertEqual(mock(), sentinel.RETURN)
开发者ID:dzohrob,项目名称:input-lib,代码行数:28,代码来源:testmock.py

示例3: test_load

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_load(self):
        with patch('pkg_resources.iter_entry_points') as iterep:
            with patch('celery.bin.base.symbol_by_name') as symbyname:
                ep = Mock()
                ep.name = 'ep'
                ep.module_name = 'foo'
                ep.attrs = ['bar', 'baz']
                iterep.return_value = [ep]
                cls = symbyname.return_value = Mock()
                register = Mock()
                e = Extensions('unit', register)
                e.load()
                symbyname.assert_called_with('foo:bar')
                register.assert_called_with(cls, name='ep')

            with patch('celery.bin.base.symbol_by_name') as symbyname:
                symbyname.side_effect = SyntaxError()
                with patch('warnings.warn') as warn:
                    e.load()
                    self.assertTrue(warn.called)

            with patch('celery.bin.base.symbol_by_name') as symbyname:
                symbyname.side_effect = KeyError('foo')
                with self.assertRaises(KeyError):
                    e.load()
开发者ID:AnSavvides,项目名称:celery,代码行数:27,代码来源:test_base.py

示例4: test_walktree_cbmock

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
def test_walktree_cbmock(sftpserver):
    '''test the walktree function, with mocked callbacks (standalone functions)
    '''
    file_cb = Mock(return_value=None)
    dir_cb = Mock(return_value=None)
    unk_cb = Mock(return_value=None)

    with sftpserver.serve_content(VFS):
        with pysftp.Connection(**conn(sftpserver)) as sftp:
            sftp.walktree('.',
                          fcallback=file_cb,
                          dcallback=dir_cb,
                          ucallback=unk_cb)
            # check calls to the file callback
            file_cb.assert_called_with('./read.me')
            thecall = call('./pub/foo2/bar1/bar1.txt')
            assert thecall in file_cb.mock_calls
            assert file_cb.call_count > 3
            # check calls to the directory callback
            assert [call('./pub'),
                    call('./pub/foo1'),
                    call('./pub/foo2'),
                    call('./pub/foo2/bar1')] == dir_cb.mock_calls
            # check calls to the unknown callback
            assert [] == unk_cb.mock_calls
开发者ID:mchruszcz,项目名称:pysftp,代码行数:27,代码来源:test_walktree.py

示例5: test_a_listener_is_passed_right_parameters

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
 def test_a_listener_is_passed_right_parameters(self):
     """"""
     listener = Mock()
     event = Event()
     event.connect(listener)
     event.fire(5, shape="square")
     listener.assert_called_with(5, shape="square")
开发者ID:justttry,项目名称:test_driven_python_development,代码行数:9,代码来源:test_event.py

示例6: test_retry

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_retry(self, sleep_moc):
        # BackupManager setup
        backup_manager = self.build_backup_manager()
        backup_manager.config.basebackup_retry_times = 5
        backup_manager.config.basebackup_retry_sleep = 10
        f = Mock()

        # check for correct return value
        r = backup_manager.retry_backup_copy(f, 'test string')
        f.assert_called_with('test string')
        assert f.return_value == r

        # check for correct number of calls
        expected = Mock()
        f = Mock(side_effect=[DataTransferFailure('testException'), expected])
        r = backup_manager.retry_backup_copy(f, 'test string')
        assert f.call_count == 2

        # check for correct number of tries and invocations of sleep method
        sleep_moc.reset_mock()
        e = DataTransferFailure('testException')
        f = Mock(side_effect=[e, e, e, e, e, e])
        with pytest.raises(DataTransferFailure):
            backup_manager.retry_backup_copy(f, 'test string')

        assert sleep_moc.call_count == 5
        assert f.call_count == 6
开发者ID:huddler,项目名称:pgbarman,代码行数:29,代码来源:test_backup.py

示例7: test_create_param_callable_returns_return_value

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
 def test_create_param_callable_returns_return_value(self, layer):
     array = numpy.array([[1, 2, 3], [4, 5, 6]])
     factory = Mock()
     factory.return_value = array
     result = layer.create_param(factory, (2, 3))
     assert (result.get_value() == array).all()
     factory.assert_called_with((2, 3))
开发者ID:317070,项目名称:nntools,代码行数:9,代码来源:test_layers.py

示例8: test_initialization_legacy

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_initialization_legacy(self, NeuralNet):
        input = Mock(__name__='InputLayer', __bases__=(InputLayer,))
        hidden1, hidden2, output = [
            Mock(__name__='MockLayer', __bases__=(Layer,)) for i in range(3)]
        nn = NeuralNet(
            layers=[
                ('input', input),
                ('hidden1', hidden1),
                ('hidden2', hidden2),
                ('output', output),
                ],
            input_shape=(10, 10),
            hidden1_some='param',
            )
        out = nn.initialize_layers(nn.layers)

        input.assert_called_with(
            name='input', shape=(10, 10))
        assert nn.layers_['input'] is input.return_value

        hidden1.assert_called_with(
            incoming=input.return_value, name='hidden1', some='param')
        assert nn.layers_['hidden1'] is hidden1.return_value

        hidden2.assert_called_with(
            incoming=hidden1.return_value, name='hidden2')
        assert nn.layers_['hidden2'] is hidden2.return_value

        output.assert_called_with(
            incoming=hidden2.return_value, name='output')

        assert out[0] is nn.layers_['output']
开发者ID:dnouri,项目名称:nolearn,代码行数:34,代码来源:test_base.py

示例9: test_diamond

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_diamond(self, NeuralNet):
        input = Mock(__name__='InputLayer', __bases__=(InputLayer,))
        hidden1, hidden2, concat, output = [
            Mock(__name__='MockLayer', __bases__=(Layer,)) for i in range(4)]
        nn = NeuralNet(
            layers=[
                ('input', input),
                ('hidden1', hidden1),
                ('hidden2', hidden2),
                ('concat', concat),
                ('output', output),
                ],
            input_shape=(10, 10),
            hidden2_incoming='input',
            concat_incomings=['hidden1', 'hidden2'],
            )
        nn.initialize_layers(nn.layers)

        input.assert_called_with(name='input', shape=(10, 10))
        hidden1.assert_called_with(incoming=input.return_value, name='hidden1')
        hidden2.assert_called_with(incoming=input.return_value, name='hidden2')
        concat.assert_called_with(
            incomings=[hidden1.return_value, hidden2.return_value],
            name='concat'
            )
        output.assert_called_with(incoming=concat.return_value, name='output')
开发者ID:dnouri,项目名称:nolearn,代码行数:28,代码来源:test_base.py

示例10: test_iterable_factory_outgoing

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_iterable_factory_outgoing(self):
        values = [Mock(name="value1", bar=1), Mock(name="value2", bar=2), Mock(name="value3", bar=3)]
        iterable = MagicMock(name="iterable")
        iterable.__iter__.return_value = iter(values)

        class MockResource(ModelResource):
            model_class = Mock()
            fields = [AttributeField(attribute="bar", type=int)]

        iterable_factory = Mock(name="iterable_factory", return_value=iterable)

        source_object = Mock(name="source_object")
        target_dict = {}

        field = IterableField(attribute="foo", resource_class=MockResource, iterable_factory=iterable_factory)

        field.handle_outgoing(mock_context(), source_object, target_dict)

        self.assertEqual(
            target_dict["foo"],
            [
                {"bar": 1, "_id": str(values[0].pk)},
                {"bar": 2, "_id": str(values[1].pk)},
                {"bar": 3, "_id": str(values[2].pk)},
            ],
        )

        iterable_factory.assert_called_with(source_object.foo)
开发者ID:wware,项目名称:savory-pie,代码行数:30,代码来源:test_fields.py

示例11: test_initialization_with_tuples

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_initialization_with_tuples(self, NeuralNet):
        input = Mock(__name__='InputLayer', __bases__=(InputLayer,))
        hidden1, hidden2, output = [
            Mock(__name__='MockLayer', __bases__=(Layer,)) for i in range(3)]
        nn = NeuralNet(
            layers=[
                (input, {'shape': (10, 10), 'name': 'input'}),
                (hidden1, {'some': 'param', 'another': 'param'}),
                (hidden2, {}),
                (output, {'name': 'output'}),
                ],
            input_shape=(10, 10),
            mock1_some='iwin',
            )
        out = nn.initialize_layers(nn.layers)

        input.assert_called_with(
            name='input', shape=(10, 10))
        assert nn.layers_['input'] is input.return_value

        hidden1.assert_called_with(
            incoming=input.return_value, name='mock1',
            some='iwin', another='param')
        assert nn.layers_['mock1'] is hidden1.return_value

        hidden2.assert_called_with(
            incoming=hidden1.return_value, name='mock2')
        assert nn.layers_['mock2'] is hidden2.return_value

        output.assert_called_with(
            incoming=hidden2.return_value, name='output')

        assert out[0] is nn.layers_['output']
开发者ID:dnouri,项目名称:nolearn,代码行数:35,代码来源:test_base.py

示例12: test_build_create

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
def test_build_create():
    population_class = Mock()
    create_function = common.build_create(population_class)
    assert isfunction(create_function)
    
    p = create_function("cell class", "cell params", n=999)
    population_class.assert_called_with(999, "cell class", "cell params")
开发者ID:agravier,项目名称:pynn,代码行数:9,代码来源:test_lowlevelapi.py

示例13: test_entry_init

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_entry_init(self, mock_entry_init):
        eset = self.get_obj()
        eset.entries = dict()
        evt = Mock()
        evt.filename = "test.txt"
        handler = Mock()
        handler.__basenames__ = []
        handler.__extensions__ = []
        handler.deprecated = False
        handler.experimental = False
        handler.__specific__ = True

        # test handling an event with the parent entry_init
        eset.entry_init(evt, handler)
        mock_entry_init.assert_called_with(eset, evt, entry_type=handler, specific=handler.get_regex.return_value)
        self.assertItemsEqual(eset.entries, dict())

        # test handling the event with a Cfg handler
        handler.__specific__ = False
        eset.entry_init(evt, handler)
        handler.assert_called_with(os.path.join(eset.path, evt.filename))
        self.assertItemsEqual(eset.entries, {evt.filename: handler.return_value})
        handler.return_value.handle_event.assert_called_with(evt)

        # test handling an event for an entry that already exists with
        # a Cfg handler
        handler.reset_mock()
        eset.entry_init(evt, handler)
        self.assertFalse(handler.called)
        self.assertItemsEqual(eset.entries, {evt.filename: handler.return_value})
        eset.entries[evt.filename].handle_event.assert_called_with(evt)
开发者ID:rcuza,项目名称:bcfg2,代码行数:33,代码来源:Test_init.py

示例14: test_initialization_with_tuples

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_initialization_with_tuples(self, NeuralNet):
        input = Mock(__name__="InputLayer", __bases__=(InputLayer,))
        hidden1, hidden2, output = [Mock(__name__="MockLayer", __bases__=(Layer,)) for i in range(3)]
        nn = NeuralNet(
            layers=[
                (input, {"shape": (10, 10), "name": "input"}),
                (hidden1, {"some": "param", "another": "param"}),
                (hidden2, {}),
                (output, {"name": "output"}),
            ],
            input_shape=(10, 10),
            mock1_some="iwin",
        )
        out = nn.initialize_layers(nn.layers)

        input.assert_called_with(name="input", shape=(10, 10))
        assert nn.layers_["input"] is input.return_value

        hidden1.assert_called_with(incoming=input.return_value, name="mock1", some="iwin", another="param")
        assert nn.layers_["mock1"] is hidden1.return_value

        hidden2.assert_called_with(incoming=hidden1.return_value, name="mock2")
        assert nn.layers_["mock2"] is hidden2.return_value

        output.assert_called_with(incoming=hidden2.return_value, name="output")

        assert out is nn.layers_["output"]
开发者ID:buyijie,项目名称:nolearn,代码行数:29,代码来源:test_base.py

示例15: test_getpids

# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import assert_called_with [as 别名]
    def test_getpids(self, gethostname, Pidfile):
        gethostname.return_value = 'e.com'
        self.prepare_pidfile_for_getpids(Pidfile)
        callback = Mock()

        p = NamespacedOptionParser(['foo', 'bar', 'baz'])
        nodes = self.t.getpids(p, 'celeryd', callback=callback)
        self.assertEqual(nodes, [
            ('foo.e.com',
             ('celeryd', '[email protected]', '-n foo.e.com', ''),
             10),
            ('bar.e.com',
             ('celeryd', '[email protected]', '-n bar.e.com', ''),
             11),
        ])
        self.assertTrue(callback.called)
        callback.assert_called_with(
            'baz.e.com',
            ['celeryd', '[email protected]', '-n baz.e.com', ''],
            None,
        )
        self.assertIn('DOWN', self.fh.getvalue())

        # without callback, should work
        nodes = self.t.getpids(p, 'celeryd', callback=None)
开发者ID:Acidburn0zzz,项目名称:firefox-flicks,代码行数:27,代码来源:test_celeryd_multi.py


注:本文中的mock.Mock.assert_called_with方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。