0
下面给出的函数:测试真实函数或模拟unittest的依赖关系?
def _extract_row(self, rnumber, rcontent):
number = rnumber.find('p').text.strip()
p = rcontent.find('p')
dloms = p.find_all('a')
docs = {}
for d in dloms:
docs[d.text.strip()] = DOMAIN + d['href']
ems = p.find_all('em')
for e in ems:
name = e.text.strip()
if not docs.get(name):
docs[name] = None
ldocs = []
for name, link in docs.iteritems():
ldocs.append({'name': name,
'link': link})
return {'number': number,
'docs': ldocs}
其中rnumber和rcontent是BS4或Beautifulsoup对象,是它更好地正常安装BS4作为测试的依赖和测试功能,或者是它更好地模拟BS4?
这里是我做过什么:
@patch("agc_law.bs4.BeautifulSoup")
def test_law_pages_private_extract_row(self, mock_bs4):
"""Test asserting correct calls are made in _extract_row"""
agc_law.DOMAIN = "http://example.com"
mock_rnumber = mock_bs4
mock_rcontent = mock_bs4
result = self.lp._extract_row(mock_rnumber, mock_rcontent)
for item in [call.find('p'),
call.find().text.strip(),
call.find('p'),
call.find().find_all('a'),
call.find().find_all('em')]:
self.assertIn(item, mock_rcontent.mock_calls)
for item in [call.find('p'),
call.find().text.strip(),
call.find('p'),
call.find().find_all('a'),
call.find().find_all('em')]:
self.assertIn(item, mock_rnumber.mock_calls)
都重视的意见。
请注意,使用'@patch(“agc_law.bs4.BeautifulSoup”)'和'@patch(“bs4.BeautifulSoup”)''是一回事。 –
谢谢你的抬头 –