【Python + Selenium】之unittest测试用例满足条件,进行跳过测试Skip

直接上代码:

__author__ = 'zc'

import unittest



class demoSkipTest(unittest.TestCase):
    a = 70
    b = 50

    print(a%b)
    def test_add(self):
        """加法"""
        result = self.a + self.b
        self.assertEqual(result, 40)

    def test_add_2(self):
        self.skipTest("强制跳过示例")
        result = self.a + self.b
        self.assertEqual(result, 9)

    @unittest.skipIf(a > b, u"a>b,正确就强制跳过")
    def test_sub(self):
        """减法"""
        result = self.a - self.b
        self.assertTrue(result == -30)

    @unittest.skipUnless(a%b == 2, u"错误就跳过")
    def test_div(self):
        """除法"""
        result = self.a / self.b
        self.assertTrue(result == 1)

    @unittest.expectedFailure
    def test_mul01(self):
        """乘法"""
        result = self.a * self.b
        self.assertTrue(result == 350)

    @unittest.expectedFailure
    def test_mul02(self):
        """乘法"""
        result = self.a * self.b
        self.assertTrue(result == 3500)


if __name__ == "__main__":
    unittest.main()

 

运行结果:

Fssxus
======================================================================
FAIL: test_add (__main__.demoSkipTest)
加法
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/xxx/run_safe_test.py", line 24, in test_add
    self.assertEqual(result, 40)
AssertionError: 120 != 40

----------------------------------------------------------------------
Ran 6 tests in 0.000s

FAILED (failures=1, skipped=3, expected failures=1, unexpected successes=1)

说明:

最后两个,先预想这个用例是错误的,但是最后一个结果是正确的,所以

expected failures=1       (预想就是错的)

unexpected successes=1   (没想到是正确的)

 

 

序号 方法 说明
1 unittest.skip(reason)  强制跳转。reason是跳转原因
2 unittest.skipIf(condition, reason)  条件跳转,如果condition是True则跳转
3 unittest.skipUnless(condition, reason) 除非conditioin为True,才进行调整
4 unittest.expectedFailure() 标记该测试预期为失败 ,如果该测试方法运行失败,则该测试不算做失败

 

 

 

 

 

 

 

 。

posted @ 2018-10-09 09:56  Owen_ET  阅读(1096)  评论(2编辑  收藏  举报