unittest.mock: asserting partial match for method argument
鲁比派在这里写Python。我有一些代码看起来有点像这样:
1 | result = database.Query('complicated sql with an id: %s' % id) |
1 | mock(database).query(/#{id}/) |
但我看不出在unittest.mock中设置"选择性模拟"的方法,至少没有一些复杂的
1 2 3 4 | with patch(database) as MockDatabase: instance = MockDatabase.return_value ... instance.Query.assert_called_once_with(re.compile("%s" % id)) |
但这也不管用。这种方法确实有效,但很难看:
1 2 3 4 | with patch(database) as MockDatabase: instance = MockDatabase.return_value ... self.assertIn(id, instance.Query.call_args[0][0]) |
更好的主意?
1 2 3 4 5 6 7 8 9 10 | import mock class AnyStringWith(str): def __eq__(self, other): return self in other ... result = database.Query('complicated sql with an id: %s' % id) database.Query.assert_called_once_with(AnyStringWith(id)) ... |
编辑:抢先要求匹配字符串
1 2 3 4 5 6 7 8 | def arg_should_contain(x): def wrapper(arg): assert str(x) in arg,"'%s' does not contain '%s'" % (arg, x) return wrapper ... database.Query = arg_should_contain(id) result = database.Query('complicated sql with an id: %s' % id) |
您只需使用
1 2 3 4 5 6 7 8 | from unittest.mock import Mock, ANY def foo(some_string): print(some_string) foo = Mock() foo("bla") foo.assert_called_with(ANY) |
如本文所述-https://docs.python.org/3/library/unittest.mock.html任何
我总是编写单元测试,以便它们反映"真实世界"。我真的不知道你想测试什么,除了
我不知道EDOCX1[1]应该做什么,但我想它应该创建一个查询对象,您可以稍后调用或传递到连接?
最好的测试方法是以现实世界为例。做一些简单的事情,比如检查ID是否出现在查询中,这太容易出错了。我经常看到人们想在单元测试中做神奇的事情,这总是导致问题。保持单元测试的简单和静态。在您的情况下,您可以:
1 2 3 4 5 6 7 8 9 10 | class QueryTest(unittest.TestCase): def test_insert_id_simple(self): expected = 'a simple query with an id: 2' query = database.Query('a simple query with an id: %s' % 2) self.assertEqual(query, expected) def test_insert_id_complex(self): expected = 'some complex query with an id: 6' query = database.Query('some complex query with an id: %s' 6) self.assertEqual(query, expected) |
如果
如果
1 2 3 | def Query(self, query): self.executeSQL(query) return query |
您可以使用
1 2 3 4 5 | @mock.patch('database.executeSQL') def test_insert_id_simple(self, mck): expected = 'a simple query with an id: 2' query = database.Query('a simple query with an id: %s' % 2) self.assertEqual(query, expected) |
作为额外的提示,尝试使用
我也觉得测试字符串格式是多余的。如果