pytest_重写pytest_sessionfinish方法的执行顺序_结合报告生成到发送邮件

背景:

Python + pytest+pytest-testreport生成测试报告,到了生成报告之后,想要发送邮件,之前的方案是配合Jenkins,配置报告的路径进行发送

如果是平时的跑的项目,没有走到Jenkins,就没办法发送邮件,导致有时测试用例跑完之后,没有发送邮件,不清楚执行的情况

鉴于这种情况,做了测试用例执行完成之后,发送邮件的操作

 

实现的效果:

pytest执行用例执行完成后,通过pytest-testreport生成测试报告后,再调起发送邮件方法,将最新的测试报告发送出去

实现思路:

1、 conftest文件,自定义pytest的参数:sendemail

2、 sendemail方法主要判断参数是否需要发邮件,需要则调用获取当前最新报告的方法,get_new_file

3、 get_new_file:主要通过获取给定文件目录,按照时间进行排序,然后获取最新的文件,最后返回最新文件的路径,将文件提供给发送邮件

4、 执行完成后,获取sendemail的参数值,如果为True,则需要发送邮件

5、 在conftest文件,定义的sendemail方法,其实是重写了pytest的pytest_sessionfinish,这个钩子函数的作用是测试用例执行完成后,再执行的一个方法

6、 由于使用了pytest-testreport,这个插件,也重写了pytest_sessionfinish,如果没有执行这两个同名钩子函数的执行顺序,则会出现,先执行了发送邮件,发送邮件取的是旧的文件,不是当前最新生成的文件,发送邮件的附件和最新的测试结果不匹配

7、 鉴于上面的问题,需要给两个同名的函数,指定执行的顺序,此时要使用@pytest.hookimpl(执行最后执行),即@pytest.hookimpl(trylast=True)

Hookimple执行顺序:

可参考:https://wenku.baidu.com/view/6f2c136be618964bcf84b9d528ea81c758f52ea5.html

这是执⾏的顺序:

1. Plugin3的pytest_collection_modifyitems被调⽤直到注⼊点,因为它是⼀个钩⼦函数装饰器。

2. 调⽤Plugin1的pytest_collection_modifyitems是因为它标有tryfirst=True。

3. 调⽤Plugin2的pytest_collection_modifyitems因为它被标记trylast=True(但即使没有这个标记,它也会在Plugin1之后出现)。

4. 插件3的pytest_collection_modifyitems然后在注⼊点之后执⾏代码。yield接收⼀个Result实例,该实例封装了调⽤⾮装饰器的结

果。包装不得修改结果。

以上使⽤tryfirst,trylast,以及结合hookwrapper=True的⽰例,它会影响彼此之间hookwrappers的排序

具体实现代码:

1、 获取当前测试报告目录下,最新的文件,并获取最新文件的路径

2、 Conftest 文件添加新的执行参数sendemail

 

def pytest_addoption(parser):  # 添加是否发送邮件的配置参数
    parser.addoption(
        "--sendemail",
        action="store",
        metavar="sendemail",
        default="true",
        help="sendemail",
    )

3、 Conftest文件重写测试用例执行完成的方法【pytest_sessionfinish】

 

@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session):
    # 在测试用例执行完成后执行,由于pytesttestreport也有使用此钩子函数,属于要加上执行的顺序trylast=True
    email = session.config.getoption('--sendemail')
    if email:
        file = CommunFun().get_new_file("D:\\xxx\\xxx\\xxx\\reports")
        sendEmail().send_email(email_to="xxxx@qq.com", filepath=file)

4、 Pytest-testport报告也使用了【pytest_sessionfinish】,所以要给本地conftest文件的【pytest_sessionfinish】,加上执行的顺序

@pytest.hookimpl(trylast=True)

posted on 2022-05-19 15:41  刚刚好1  阅读(948)  评论(0编辑  收藏  举报

导航