我在python中有以下测试用例。
class RulesTest(TestCase):
request_length = 484
url = "http://www.ndtv.com"
def setUp(self):
har_data = open('/Users/rokumar/SiteAnalysisGit/Src/hct/hct/test.har')
self.data = json.load(har_data)
self.rule = models.Rule(name=RULES.DNS,user=None,
threshold=None)
self.rule.save()
def tearDown(self):
self.rule.delete()
def test_parse_har(self):
with patch.object(rules, 'add_dns_analysis', return_value=None) as \
dns_mock:
dns_mock.add_dns_analysis('test result', 'test url')
result = rules.parse_har(self.data,[self.rule],RulesTest.url)
self.assertEqual(result[RULES.TOTAL_REQUESTS], 484)
self.assertEqual(result[RULES.HTML_SIZE], 241592)
self.assertEqual(result[RULES.JS_SIZE], 1006469)
dns_mock.assert_called_once_with('test result', 'test url')
最后一个断言无法告知实际的呼叫已经完成。为什么python没有绕过这个调用。
答案 0 :(得分:1)
也许您想检查方法add_dns_analysis
是否被parse_har()
调用:
将您的测试重写为
def test_parse_har(self):
with patch.object(rules, 'add_dns_analysis', return_value=None) as \
dns_mock:
result = rules.parse_har(self.data,[self.rule],RulesTest.url)
self.assertEqual(result[RULES.TOTAL_REQUESTS], 484)
self.assertEqual(result[RULES.HTML_SIZE], 241592)
self.assertEqual(result[RULES.JS_SIZE], 1006469)
dns_mock.assert_called_once_with('test result', 'test url')