• pytest二:setup和teardown


    用例运行级别
      模块级(setup_module/teardown_module)开始于模块始末,全局的
      函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
      类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
      方法级(setup_method/teardown_method)开始于方法始末(在类中)
      类里面的(setup/teardown)运行在调用方法的前后

    函数级:function与module

    setup_function/teardown_function 每个用例开始和结束调用一次

    从结果可以看出用例执行顺序:
    setup_function》用例 1》teardown_function,setup_function》用例2》teardown_function》setup_function》用例 3》teardown_function

    import pytest

    def setup_function():
    print("setup_function:每个用例开始前都会执行")

    def teardown_function():
    print("teardown_function:每个用例结束后都会执行")

    def test_one():
    print("正在执行--test_one")
    x = 'this'
    assert 'h' in x

    def test_two():
    print("正在执行--test_one")
    x = 'hello'
    assert hasattr(x, 'check')

    def test_three():
    print("正在执行--test_one")
    a = 'hello'
    b = 'hello world'
    assert a in b

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

    setup_module/teardown_module

    setup_module 所有用例开始前执行一次,teardown_module 所有用例结束后执行一次

     

    从运行结果可以看到 setup_module 和 teardown_module 整个.py 模块只执行了一次

    import pytest

    def setup_module():
    print('setup_module:整个.py模块只执行一次')
    print('比如:所有用例开始前只打开一次浏览器')

    def teardown_module():
    print('setup_module:整个.py模块只执行一次')
    print('比如:所有用例结束后关闭浏览器')

    def setup_function():
    print("setup_function:每个用例开始前都会执行")

    def teardown_function():
    print("teardown_function:每个用例结束后都会执行")

    def test_one():
    print("正在执行--test_one")
    x = 'this'
    assert 'h' in x

    def test_two():
    print("正在执行--test_one")
    x = 'hello'
    assert hasattr(x, 'check')

    def test_three():
    print("正在执行--test_one")
    a = 'hello'
    b = 'hello world'
    assert a in b

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

     类和方法级:

    setup_class/teardown_class:
    setup_class 和 teardown_class 等价于 unittest 里面的setupClass 和 teardownClass

    从结果看出,运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class

    import pytest

    class TestCase():

    def setup(self):
    print("setup:每个用例开始前都会执行")

    def teardown(self):
    print("teardown:每个用例结束后都会执行")

    def setup_class(self):
    print('setup_class:所有用例执行之前')

    def teardown_class(self):
    print('setup_class:所有用例执行之后')

    def setup_method(self):
    print('setup_method:每个用例开始前执行')

    def teardown_method(self):
    print('teardown_method:每个用例结束后执行')

    def test_one(self):
    print("正在执行--test_one")
    x = 'this'
    assert 'h' in x

    def test_two(self):
    print("正在执行--test_one")
    x = 'hello'
    assert hasattr(x, 'check')

    def test_three(self):
    print("正在执行--test_one")
    a = 'hello'
    b = 'hello world'
    assert a in b

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

     函数和类的混合:

    一个.py 的文件里面既有函数用例又有类和方法用例

    从运行结果看出,setup_module/teardown_module 的优先级是最大的,然后函数里面用到的 setup_function/teardown_function与类里面的 setup_class/teardown_class 互不干涉

    import pytest

    def setup_module():
    print('setup_module:整个.py模块只执行一次')
    print('比如:所有用例开始前只打开一次浏览器')

    def teardown_module():
    print('setup_module:整个.py模块只执行一次')
    print('比如:所有用例结束后关闭浏览器')

    def setup_function():
    print("setup_function:每个用例开始前都会执行")

    def teardown_function():
    print("teardown_function:每个用例结束后都会执行")

    def test_one():
    print("正在执行--test_one")
    x = 'this'
    assert 'h' in x

    def test_two():
    print("正在执行--test_one")
    x = 'hello'
    assert hasattr(x, 'check')

    class TestCase():

    def setup_class(self):
    print('setup_class:所有用例执行之前')

    def teardown_class(self):
    print('setup_class:所有用例执行之后')

    def test_one(self):
    print("正在执行--test_one")
    x = 'this'
    assert 'h' in x

    def test_two(self):
    print("正在执行--test_one")
    x = 'hello'
    assert hasattr(x, 'check')

    def test_three(self):
    print("正在执行--test_one")
    a = 'hello'
    b = 'hello world'
    assert a in b

    if __name__=='__main__':
    pytest.main()
  • 相关阅读:
    convirt2.5在虚拟机上安装笔记
    myeclipse 自动提示JS
    Hadoop学习之HBase基础知识、操作和原理
    Hadoop学习之HBase的集群环境搭建
    学习面向对象之异常之三
    java学习面向对象之异常之二
    java学习面向对象之异常之一
    java学习面向对象之匿名内部类
    java学习面向对象之内部类
    java学习面向对象之多态
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/9675970.html
Copyright © 2020-2023  润新知