美文网首页
Pytest学习3 -setup和teardown的使用

Pytest学习3 -setup和teardown的使用

作者: C1R2 | 来源:发表于2020-10-31 21:55 被阅读0次
    unittest用法

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

    setup()
    setupClass()
    teardown()
    teardownClass()
    
    pytest用法

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

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

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

    # -*- coding: utf-8 -*-
    
    # @FileName: test_setup_teardown_unittest.py
    
    '''
    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.assertEqual(1, 1)
    
        def testB(self):
            '''用例B'''
            print("用例B执行了")
            self.assertTrue(True)
    
    
    if __name__ == "__main__":
        unittest.main()
    
    

    执行结果


    图片.png

    可以看出执行顺序为:

    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 -*-
    
    # @FileName: test_setup_teardown_pytest.py
    
    '''
    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"])
    
    

    执行结果


    图片.png

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

    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打印测试用例的执行结果级别。
    

    参考链接
    https://www.cnblogs.com/longronglang/p/13854850.html
    系列参考文章:
    https://www.cnblogs.com/poloyy/category/1690628.html

    相关文章

      网友评论

          本文标题:Pytest学习3 -setup和teardown的使用

          本文链接:https://www.haomeiwen.com/subject/uphdvktx.html