用美化的方法查找测试用例和结果

2024-04-25 05:41:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个很好的方法来找到所有测试用例的名称和html文件中每个测试用例的结果。我是新来美容的,需要一些好的建议。在

首先,我使用BeautifulSoup读取数据并对其进行修饰并将其放入一个文件中:

from bs4 import BeautifulSoup
f = open('myfile','w')
soup = BeautifulSoup(open("C:\DEV\debugkod\data.html"))
fixedSoup = soup.prettify()
fixedSoup = fixedSoup.encode('utf-8')
f.write(fixedSoup)
f.close()

当我检查文件中的prettify result中的部件时,例如它将如下所示(该文件包括100个tc和结果):

^{2}$

在这个文件中,我现在想找到关于“名称”和“结果状态:”的信息。如果检查美化结果,我可以看到标记“Name:”和“result state:”。希望可以使用它们来查找测试用例名称和测试结果。。。所以打印输出应该是这样的:

 Name = IAA REQPROD 55 InvPwrDownMode - Shut down communication 
 Result = Passed
 etc

有人知道怎么用beauthoulsoup来做这个吗?在


Tags: 文件方法name名称html测试用例resultopen
1条回答
网友
1楼 · 发布于 2024-04-25 05:41:44

使用html from your second Pastebin link,以下代码:

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("beautifulsoup2.html"))


names = []
for table in soup.findAll('table', attrs={'class': 'Title'}):
    td = table.find('td')
    names.append(td.text.encode("ascii", "ignore").strip())

results = []
for table in soup.findAll(attrs={'class': 'StaticAttributes'}):
    tds = table.findAll('td')
    results.append(tds[1].text.strip())

for name, result in zip(names, results):
    print "Name = {}".format(name)
    print "Result = {}".format(result)
    print

给出这个结果:

^{pr2}$

我添加了encode("ascii", "ignore"),否则我会得到UnicodeDecodeError。请参阅this answer,了解这些字符在html中的最终结果。在

相关问题 更多 >