python_unittest套件及装饰器

固定套件:

  • unittest module包含了编写运行unittest的功能,自定义的test class都要集成unitest.TestCase类,test method要以test开头,运行顺序根据test method的名字排序,特殊方法:
    setup():每个测试函数运行前运行
    teardown():每个测试函数运行完后执行
    setUpClass():必须使用@classmethod装饰器,所有test运行前运行一次
    tearDownClass():必须使用@classmethod装饰器,所有test运行完后运行一次

装饰器

  • 可以使用unitest.skip装饰器族跳过test method或者test class,这些装饰器包括:
    @unittest.skip(reason):无条件跳过测试,reason描述为什么跳过测试
    @unittest.skipif(conditition,reason):condititontrue时跳过测试: 这里完全可以应用条件去控制用例是否执行了,很灵活
    @unittest.skipunless(condition,reason):condition不是true时跳过测试
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

@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
  def test_not_run(self):
    pass
  • expected failure:使用@unittest.expectedFailure装饰器,如果test失败了,这个test不计入失败的case数目

@阴天-2017-01-17 17:50:40

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,711评论 19 139
  • 单元测试 什么是单元 单元测试(unit testing),是指对软件中的最小可测试单元(一个模块、一个函数或者一...
    PPMac阅读 11,657评论 0 19
  • Startup 单元测试的核心价值在于两点: 更加精确地定义某段代码的作用,从而使代码的耦合性更低 避免程序员写出...
    wuwenxiang阅读 13,410评论 1 27
  • 用Python搭建自动化测试框架,我们需要组织用例以及测试执行,这里博主推荐Python的标准库——unittes...
    灰蓝蓝蓝蓝蓝蓝阅读 13,242评论 3 56
  • 以为一些事不想就不存在 以为一些人会遗忘 忽然一些人和事 像演电影一样在脑海里反复播放 回想过往点点滴滴 心中不免...
    田萍阅读 897评论 0 7