背景:在执行单元测试中,有些方法执行耗时,不想全部执行,想忽略执行,那就需要跳过某方法执行

1.无条件跳过某方法

@unittest.skip("skipping")

2.使用变量的方式,指定忽略测试方法

a=10
@unittest.skipIf(a > 5, "condition is not satisfied!")

表示if a>5为真,就跳过此方法

 

3.指定测试平台忽略测试方法  

@unittest.skipUnless(sys.platform.startswith("Linux"), "requires Linux")

  如果不是liunx ,就直接跳过,python可以使用sys,查看自己的平台信息

unittest指定跳过某些方法

 

 测试方法

import random
import unittest
import sys

class TestSequenceFunctions(unittest.TestCase):
    a = 10
    def setUp(self):
        self.seq = list(range(10))

    @unittest.skip("skipping") # 无条件忽略该测试方法
    def test_shuffle(self):
        random.shuffle(self.seq)
        self.seq.sort()
        self.assertEqual(self.seq, list(range(10)))
        self.assertRaises(TypeError, random.shuffle, (1, 2, 3))

    # 如果变量a > 5,则忽略该测试方法
    @unittest.skipIf(a > 5, "condition is not satisfied!")
    def test_choice(self):
        element = random.choice(self.seq)
        self.assertTrue(element in self.seq)

    # 除非执行测试用例的平台是Linux平台,否则忽略该测试方法  win32是windows
    @unittest.skipUnless(sys.platform.startswith("Linux"), "requires Linux")
    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)


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

  测试结果

unittest指定跳过某些方法

 

 

 


 
                    
            
                

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2022-01-07
  • 2022-12-23
  • 2022-02-02
相关资源
相似解决方案