zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    [转]进程与线程及其区别
    [转]工厂模式
    [转]Filter实现处理中文乱码,转义html标签,过滤敏感词
    [转]JAVA设计模式之单例模式
    [转]Servlet 中文乱码问题及解决方案剖析
    Servlet作业2-将表单提交的商品信息输出到页面中
    Servlet作业1-实现注册登录
    [转] ServletContext 与application的异同
    [转]servlet中的service, doGet, doPost方法的区别和联系
    [转]Servlet 3.0 新特性详解
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/9675970.html
Copyright © 2011-2022 走看看