zoukankan      html  css  js  c++  java
  • Pytest学习(三)

    一、前言

    从文章标题可以看出,就是初始化和释放的操作,根据我的java习惯来学习pytest,个人感觉没差太多,理解上也不是很难。

    哦,对了,差点跑题了,这个框架是基于Python语言的,在学习的时候难免总会用java的类比思想来学习,下面言归正传哈。

    我们还从 unittest与pytest来对比学习吧

    二、unittest用法

    unittest有两个前置方法,两个后置方法,分别是:

    • setup()
    • setupClass()
    • teardown()
    • teardownClass()

    个人始终觉得unittest和Junit像极了。

    三、pytest用法

    当然,Pytest也提供了类似setup、teardown的方法,分别是:

    • 模块级别:setup_module、teardown_module
    • 函数级别:setup_function、teardown_function,不在类中的方法
    • 类级别:setup_class、teardown_class
    • 方法级别:setup_method、teardown_method
    • 方法细化级别:setup、teardown

    我总感觉学习pytest像是在学习testng一样,难道是我的错觉吗,啊啊啊啊,不能吧。

    四、unittest示例

    unittest的setupClass和teardownClass,需要配合@classmethod装饰器一起使用,也就是我们java说的注解呀,这块是翻译给java学Python的同学的,可忽略哈。
    示例代码如下:

    # -*- coding: utf-8 -*-
    # @Time    : 2020/10/21 20:09
    # @Author  : longrong.lang
    # @FileName: test_setup_teardown_unittest.py
    # @Software: PyCharm
    # @Cnblogs :https://www.cnblogs.com/longronglang
    '''
    unittest代码示例
    '''
    import unittest
    
    
    class TestUnitTest(unittest.TestCase):
        @classmethod 
        def setUpClass(cls):
            print("所有用例执行前执行")
    
        def setUp(self):
            print("每个用例开始前执行")
    
        def tearDown(self):
            print("每个用例结束后执行")
    
        @classmethod
        def tearDownClass(cls):
            print("所有用例执行后执行")
    
        def testA(self):
            '''用例A'''
            print("用例A执行了")
            self.assertEquals(1, 1)
    
        def testB(self):
            '''用例B'''
            print("用例B执行了")
            self.assertTrue(True)
    
    
    if __name__ == "__main__":
        unittest.main()
    
    

    执行结果

    可以看出执行顺序为:

    setUpClass
    setUp
    testA 
    tearDown
    setUp
    testB
    tearDown
    tearDownClass
    用例之间按用例名称ASCII码的顺序加载,数字与字母顺序为0~9,A~Z,a~z,  所以testA会在testB之前运行。
    

    五、pytest示例

    函数级的setup_function、teardown_function只对函数用例生效,而且不在类中使用

    依旧还是把类和函数都有的情况放在一起,示例代码如下:

    # -*- coding: utf-8 -*-
    # @Time    : 2020/10/21 20:27
    # @Author  : longrong.lang
    # @FileName: test_setup_teardown_pytest.py
    # @Software: PyCharm
    # @Cnblogs :https://www.cnblogs.com/longronglang
    '''
    pyetest示例
    '''
    
    import pytest
    
    
    def setup_module():
        print("setup_module():在模块最之前执行,且只执行一次")
    
    
    def teardown_module():
        print("teardown_module:在模块之后执行,且只执行一次")
    
    
    def setup_function():
        print("setup_function():每个方法之前执行")
    
    
    def teardown_function():
        print("teardown_function():每个方法之后执行")
    
    
    def test_1():
        print("正在执行用例1")
        x = "this"
        assert 'h' in x
    
    
    def test_2():
        print("正在执行用例2")
        assert 1 == 1
    
    
    class TestClass(object):
    
        def setup_class(self):
            print("setup_class(self):每个类之前执行一次,只执行一次")
    
        def teardown_class(self):
            print("teardown_class(self):每个类之后执行一次,只执行一次")
    
        def test_A(self):
            print("正在执行用例A")
            x = "this"
            assert 'h' in x
    
        def test_B(self):
            print("正在执行B")
            assert 1 == 1
    
    
    if __name__ == "__main__":
        pytest.main(["-q", "test_setup_teardown_pytest.py"])
    
    

    执行结果

    可以看出来,互不影响,执行顺序为:

    setup_module()
    setup_function()
    test_1
    teardown_function()
    setup_function()
    test_2
    teardown_function()
    setup_class(self)
    test_A
    test_B
    teardown_class(self)
    teardown_module
    

    main方法中的-q,为pytest打印测试用例的执行结果级别。

    如不清楚,请移步到《Pytest学习(一)- 入门及基础》。

    系列参考文章:
    https://www.cnblogs.com/poloyy/category/1690628.html

    优秀不够,你是否无可替代

    软件测试交流QQ群:721256703,期待你的加入!!

    欢迎关注我的微信公众号:软件测试君


  • 相关阅读:
    webpack高级概念code splitting 和 splitChunks (系列五)
    webpack高级概念Develoment 和 Production 不同环境的配置 (系列四)
    webpack高级概念Tree Shaking (树摇)(系列三)
    HarmonyOS三方件开发指南(16)-VideoCache 视频缓存
    鸿蒙开源第三方组件——uCrop_ohos图片裁剪组件
    Hi3516如何连接Wifi(三)
    【鸿蒙学院】鸿蒙IDE迎来重大更新,新特性足以让你尖叫
    《鸿蒙系统物联网模组——Neptune 三天全攻略》课件、代码
    预览器和编辑器双重发力,DevEco Studio 2.1 Beta 3强势来袭
    强大的鸿蒙开发环境 —— DevEco Studio 2.1 Beta3发布
  • 原文地址:https://www.cnblogs.com/longronglang/p/13854850.html
Copyright © 2011-2022 走看看