记Selenium+Unittest+HTMLTestRunner自动化测试中一次意外的跳过执行

1  背景

        当前框架采用Selenium+Unittest+HTMLTestRunner,执行用例前先汇总收集所有要执行的用例,再统一执行。        

def all_testcase():
    # 待执行测试用例的目录
    case_dir = ".\\TestCase"
    testcase = unittest.TestSuite()
    discover = unittest.defaultTestLoader.discover(case_dir, pattern="*.py", top_level_dir=None)
    # discover筛选出来的用例,添加到测试套件中
    for test_suite in discover:
        for test_case in test_suite:
            # 添加测试用例到 testcase
            testcase.addTest(test_case)
    print(testcase)
    return testcase



if __name__ == "__main__":
    path = ".\\report"
    isExists = os.path.exists(path)
    if not isExists:
        os.mkdir(path)

    nowtime = datetime.now().strftime("%Y.%m.%d.%H%M%S.%f")[:-3]
    rtime = datetime.now().strftime("%Y.%m.%d %H:%M")
    report_path = ".\\report\\result_%s" % nowtime
    os.mkdir(report_path)
    report_file = report_path + "\\result_%s.html" % nowtime
    fp = open(report_file, "wb")
    runner = HTMLTestRunner(stream=fp, title=u'测试报告', description=u'用例执行情况:')
    # 运行用例
    runner.run(all_testcase())

2  事件

        在一次查看测试报告时,发现了一个问题,被依赖的测试用例执行通过了,但依赖的用例没有执行,跳过了。

    跟踪后发现,原来是另一个测试类中有同名的测试用例跳过了,导致本该执行的用例没有执行。

3  分析解决

    因为在此框架中是重写了测试跳过方法,因本人对Unittest研究有限,没有在此方法中找到解决办法。

    重写的方法代码如下:

def skip_dependon(depend=""):
    """
    :param depend: 依赖的用例函数名,默认为空
    :return: wraper_func
    """

    def wraper_func(test_func):
        @wraps(test_func)  # @wraps:避免被装饰函数自身的信息丢失
        def inner_func(self):
            if depend == test_func.__name__:
                raise ValueError("{} cannot depend on itself".format(depend))
            # print("self._outcome", self._outcome.__dict__)
            # 此方法适用于python3.4 +
            # 如果是低版本的python3,请将self._outcome.result修改为self._outcomeForDoCleanups
            # 如果你是python2版本,请将self._outcome.result修改为self._resultForDoCleanups
            failures = str([fail[0] for fail in self._outcome.result.failures])
            errors = str([error[0] for error in self._outcome.result.errors])
            skipped = str([error[0] for error in self._outcome.result.skipped])
            flag = (depend in failures) or (depend in errors) or (depend in skipped)
            if failures.find(depend) != -1:
                # 输出结果 [<__main__.TestDemo testMethod=test_login>]
                # 如果依赖的用例名在failures中,则判定为失败,以下两种情况同理
                # find()方法:查找子字符串,若找到返回从0开始的下标值,若找不到返回 - 1
                test = unittest.skipIf(flag, "{} failed".format(depend))(test_func)
            elif errors.find(depend) != -1:
                test = unittest.skipIf(flag, "{} error".format(depend))(test_func)
            elif skipped.find(depend) != -1:
                test = unittest.skipIf(flag, "{} skipped".format(depend))(test_func)
            else:
                test = test_func
            return test(self)

        return inner_func

    return wraper_func

    后来,只能通过更改各测试类中的测试用例名,使各测试用例名称唯一来避免。

以上,记之。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zljun8210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值