gue*_*tli 5 html python testing unit-testing
我想比较 python 单元测试中包含 html 的两个字符串。
是否有一种方法可以以人类友好(类似差异)版本输出结果?
我(问这个问题的人)现在使用 BeautfulSoup:
def assertEqualHTML(string1, string2, file1='', file2=''):
u'''
Compare two unicode strings containing HTML.
A human friendly diff goes to logging.error() if there
are not equal, and an exception gets raised.
'''
from BeautifulSoup import BeautifulSoup as bs
import difflib
def short(mystr):
max=20
if len(mystr)>max:
return mystr[:max]
return mystr
p=[]
for mystr, file in [(string1, file1), (string2, file2)]:
if not isinstance(mystr, unicode):
raise Exception(u'string ist not unicode: %r %s' % (short(mystr), file))
soup=bs(mystr)
pretty=soup.prettify()
p.append(pretty)
if p[0]!=p[1]:
for line in difflib.unified_diff(p[0].splitlines(), p[1].splitlines(), fromfile=file1, tofile=file2):
logging.error(line)
raise Exception('Not equal %s %s' % (file1, file2))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3243 次 |
| 最近记录: |