在UI自动化测试中使用flaky插件运行失败用例

在UI自动化测试中,有时候经常会提示跑用例失败,在单步或单个用例调试时,用例却成功,这个失败的因素主要有环境、代码或前端定位等原因。

可以看这篇文章《我们是如何让UI测试变得稳定的》中有详细说明,但是,在这里,我们不讨论问题,我们来看看优化

项目中原来的自动化框架是基本nose的,nose中有一个选项为:

--failed              Run the tests that failed in the last test run.

可以单独运行上次测试中失败的用例,但貌似与我的想法有点背离,我的需求是失败后再运行一次用例,然后报告可以正常输出

无独有偶,正好看到有一个flaky可以支持这种需求,具体的介绍和安装,有前辈写了:http://blog.csdn.net/liuchunming033/article/details/45916389

但在nose框架中有点差异,如下:

#coding:utf-8
'''
Created on 2016年6月22日

@author: huzq
'''
from  nose.tools import with_setup
import logging
from test_case import new
from nose.tools import ok_
from nose.tools import eq_
import nose
import os
from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest
import sys
from pdb import set_trace

#TODO:jfjfjf
log = logging.getLogger(__name__)


@attr(mode=1) 
def test_learn_1():
    u'''测试取消'''
    print 'xxx'
    #raise SkipTest
    #print "test_lean_1"
    #pass
    #assert 1==2
    eq_(6, 7, msg=u"错误")

@attr(mode=2) 
def test_lean_2():
    u'''测试失败'''
    try:
        print "test_learn_2"
        ok_(4==3,msg="xxx")
        print sys._getframe().f_code.co_name
    except Exception:
        print sys._getframe().f_code.co_name
        
    
@attr(mode=2) 
def test_lean_3():
    u'''测试成功'''
    pass
    
 
def setUp():
    #set_trace()
    print "0001 test setUp"
    
def tearDown():
    print "0001 test teardown"

使用flaky方法如下:

E:workspace
osetest_lear	est_case>nosetests -v test_case_0001.py --with-flaky   #带flaky运行,默认运行一次,最后会输出flaky简要报告

E:workspace
osetest_lear	est_case>nosetests -v test_case_0001.py --with-flaky  --no-flaky-report    #没有flaky报告输出

E:workspace
osetest_lear	est_case>nosetests -v test_case_0001.py --with-flaky --no-flaky-report  --max-runs=3 --min-passes=1 #成功后只运行一次,失败后运行2次

  

原文地址:https://www.cnblogs.com/landhu/p/6237249.html