python nose测试框架全面介绍十---用例的跳过
生活随笔
收集整理的這篇文章主要介紹了
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 unittestclass 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 unittestclass TestClass():@classmethoddef setUpClass(self):print "xxxxxx"def setUp(self):print "MyTestClass setup"def tearDown(self):print "MyTestClass teardown"@SkipTestdef 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 codepass?
轉載于:https://www.cnblogs.com/landhu/p/9200969.html
總結
以上是生活随笔為你收集整理的python nose测试框架全面介绍十---用例的跳过的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NetworkX系列教程(1)-创建gr
- 下一篇: centos7 git安装