习惯性伸手党区 unittest.skipIf 如何获取判断参数

Mir.Chen · 2017年12月24日 · 最后由 wangliang 回复于 2019年01月21日 · 2682 次阅读
import unittest
class A(unittest.TestCase):

    _result = {}

    def test_001(self):
        self.x = 1
        if self.x >= 0:
            self._result['test_001'] = 'pass'

    @unittest.skipIf(_result['test_001'] == 'pass', '跳过')
    def test_002(self):
        pass

if __name__ == 'main':
    unittest.main()

实际上字典中没有 ['test_001'导致报错:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Administrator/PycharmProjects/Appium_auto_test/test.py
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/Appium_auto_test/test.py", line 2, in <module>
    class A(unittest.TestCase):
  File "C:/Users/Administrator/PycharmProjects/Appium_auto_test/test.py", line 11, in A
    @unittest.skipIf(_result['test_001'] == 'pass', '跳过')
KeyError: 'test_001'

那么@unittest.skIf() 如何实现对业务逻辑判断呢?

共收到 2 条回复 时间 点赞

我是这样实现的
import sys ,unittest
reload(sys)
sys.setdefaultencoding("utf-8")
class A(unittest.TestCase):
result = {'test_001':False}
@classmethod
def setUpClass(cls):
super(A, cls).setUpClass()
def test_001(self):
self.result['test_001'] = True

print self.result
#@unittest.skipIf(result['a'] == 4, '跳过')
def test_002(self):
if self.result['test_001'] == True:
return
print 'test_002'
if name == 'main':
unittest.main()

import sys ,unittest
reload(sys)
sys.setdefaultencoding("utf-8")
class A(unittest.TestCase):
    result = {'test_001':False}
    @classmethod
    def setUpClass(cls):
        super(A, cls).setUpClass()
    def test_001(self):
        self.result['test_001'] = True

        print self.result
    #@unittest.skipIf(result['a'] == 4, '跳过')
    def test_002(self):
        if self.result['test_001'] == True:
            return
        print 'test_002'
if __name__ == '__main__':
    unittest.main()
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册