测试报告、BeautifulSoup解析、json判断相等

一、之前有写的一篇文章:https://www.cnblogs.com/yinwenbin/p/10596227.html

里面的测试报告是从网上找的,虽然有各种各样的版本,但是内核是不变的。

二、json判断相等

群友出的问题,数据如下

    a = [{'start': 1.0, 'end': 11.0},
         {'start': 1.0, 'end': 11.0},
         {'start': 3.0, 'end': 33.0},
         {'start': 4.0, 'end': 5.0}]

    b = [{'start': 3.0, 'end': 33.0},
         {'start': 1.0, 'end': 11.0},
         {'start': 1.0, 'end': 11.0},
         {'start': 4.0, 'end': 5.0}]

一开始我认为判断list、dict递归的方式比较没毛病,然后发现网上写的比较都有问题。

然后终于找到一个靠谱的:https://blog.csdn.net/qq_27884799/article/details/93904318

安装:pip install jsoncomparedeep

使用方式:

    from json_compare import Jcompare
    re=Jcompare().compare(a,b)
    print(re)

三、见到有人用BeautifulSoup解析自动化结果邮件来获取成功失败数,挺有意思的。之前听过BeautifulSoup,但是没有用过,借这个例子记录下

<p><strong>Start Time:</strong> 2021-03-12 18:19:13</p>
<p><strong>Duration:</strong> 0:00:00.001656</p>
<p><strong>Status:</strong> <span class="text text-success">Pass <strong>1</strong></span> <span class="text text-danger">Failure <strong>1</strong></span> <span class="text text-warning">Error <strong>1</strong></span></p>
from bs4 import BeautifulSoup
import re


def test(f):
    try:
        html = open(f, "r", encoding="utf-8").read()
        if html:
            soup = BeautifulSoup(html, "html5lib")
            # 使用find的方式
            # success= int(soup.find("td", class_="text text-success").text)
            # 使用select的方式
            all = soup.select("strong")
            if all[3]:
                success = int(re.search(r">(.*)<", str(all[3])).group()[1])
            if all[4]:
                fail = int(re.search(r">(.*)<", str(all[4])).group()[1])
            if all[5]:
                error = int(re.search(r">(.*)<", str(all[4])).group()[1])
            total = success + fail + error
            rate = '%.2f%%' % (float(success) / float(total) * 100)
            print(total, fail, error, success, rate)

            return total, fail, error, success, rate
        else:
            return "执行异常,报告内容为空"
    except FileNotFoundError:
        return "执行异常,报告目录不存在"


if __name__ == '__main__':
    test("20210312181913.html")

 

posted @ 2021-01-05 18:37  whitewall  阅读(174)  评论(0)    收藏  举报