常见问题(四)测试报告中,被装饰器装饰的测试用例,没有描述

如下图(前两个测试用例,没有用例描述):

 原因是,前两个测试用例,添加了用例失败后截图的装饰器,而装饰器会重写被装饰函数的名称和docstring

    @Screen(driver)  # 添加装饰器修饰,失败时自动截图
    def test_search_2(self):
        '''搜索关键词selenium'''   # 这一句描述,在测试报告中显示不出来
        self.driver.get(self.base_url)
        self.page.search_input.send_keys('selenium')
        self.page.search_button.click()
        time.sleep(2)
        self.assertEqual(self.driver.title, 'selenium_百度搜索')
        time.sleep(2)

修改截图装饰器,如下:

import time
from functools import wraps   # 导入wraps包


class Screen(object):
    def __init__(self, driver):
        self.driver = driver

    def __call__(self, f):
        @wraps(f)    # 使用wraps包处理,保证被装饰的函数,函数名和docstring不被重写(生成的测试报告中,有正确的docstring描述)
        def inner(*args):
            try:
                return f(*args)
            except Exception:
                img_path = './test_image/'
                now_time = time.strftime('%Y_%m_%d_%H_%M_%S')
                self.driver.get_screenshot_as_file(img_path + now_time + '.png')
                raise
        return inner

好了,运行一下,结果如下:

 

 ok~

 

posted @ 2020-05-07 11:01  小虫虫大虫虫  阅读(252)  评论(0编辑  收藏  举报