zoukankan      html  css  js  c++  java
  • python nose测试框架全面介绍十---用例的跳过

      又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细。下面详细解析下

    nose自带的SkipTest

       先看看nose自带的SkipTest典型应用

       应用一:

    ‘''
    @auth:hu
    ’''
    from nose.plugins.skip import SkipTest
    @attr(mode=1) 
    def test_learn_1():
        raise SkipTest

    但这种SkipTest在实际的日志中没有显示Skip关键字

    应用二:

    如果想要在执行用例前判断相关字雄姿英发再进行用例跳过,如下

    '''
    
    @author: huzq
    '''
    import nose
    import  nose.plugins.multiprocess
    from testtools import TestCase
    from nose import SkipTest
    from nose.plugins.skip import Skip
    import unittest
    
    
    class TestClass():
     
        
        
        
        def setUp(self):
            print "MyTestClass setup"
            if "xxx" in "qqqq":
                raise SkipTest("adfdfd")
            
    
       
        def Testfunc1(self):
            print "this is Testfunc01"
            

    每个用例前都会进行判断,并进行跳过操作

    应用三:

    可用觉得写在代码中太麻烦,SkipTest也可以当装饰器来进行,如下

    '''
    @author: huzq
    '''
    import nose
    import  nose.plugins.multiprocess
    from testtools import TestCase
    from nose import SkipTest
    from nose.plugins.skip import Skip
    import unittest
    
    
    class TestClass():
      
        
        @classmethod
        def setUpClass(self):
            print "xxxxxx"
    
        
        def setUp(self):
            print "MyTestClass setup"
            
    
        def tearDown(self):
            print "MyTestClass teardown"
            
        @SkipTest
        def Testfunc1(self):
            print "this is Testfunc01"
            

    要注意的时,如果用装饰器形式,SkipTest后不能接具体原因,如果要接具体原因,只能用unittest的方法写,如下

    @unittest.skip("I don't want to run this case.")

    SkipTest可放在SetUpclass、SetUp、Test中

    使用unittest的skip


    什么都是没有完美的,如果想用装饰器来进行用例判断并跳过,nose自带的skiptest没法完成。好在unittest有更好的解决方案。

    就直接贴官网的例子吧,nose也支持 

    class MyTestCase(unittest.TestCase):
    
        @unittest.skip("demonstrating skipping")
        def test_nothing(self):
            self.fail("shouldn't happen")
    
        @unittest.skipIf(mylib.__version__ < (1, 3),
                         "not supported in this library version")
        def test_format(self):
            # Tests that work for only a certain version of the library.
            pass
    
        @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
        def test_windows_support(self):
            # windows specific testing code
            pass
  • 相关阅读:
    转载 cglib代理和java代理
    解决流不能重复使用
    @RestController的方法中 路径参数带.(点号)配置
    Spring中application*的使用
    转载自用学习 侵权删
    转载学习 多线程中的内存模型和关键字
    转载 幂等的使用
    转载学习 关于线程池
    FastJSON 转换List<T> ,Map<T,T>泛型失败 处理方法
    quartz报错 Couldn't retrieve job because the BLOB couldn't be deserialized: null
  • 原文地址:https://www.cnblogs.com/landhu/p/9200969.html
Copyright © 2011-2022 走看看