我正在尝试在单元测试中模拟装饰器参数的值。
代码:
# decorator
def get_data(soup, tag, tipo):
def valid(func):
def inner(*args):
return soup.findAll(tag, {"class": tipo})
return inner
return valid
@get_data(soup, "h2", "product__title")
def gerar_nomes():
return value
单元测试:
self.soup = BeautifulSoup(self.code, 'html.parser')
self.codnomes = desa.generate_names(self.soup)
def test_generate_names(self):
self.assertEqual('<h2 class="product__title">p1</h2>', str(self.codnomes[0]))
self.assertEqual('<h2 class="product__title">p2</h2>', str(self.codnomes[1]))