首页 > 其他 > 详细

pytest第二天-用例的setup和teardown

时间:2020-12-14 14:45:46      阅读:22      评论:0      收藏:0      [点我收藏+]

用例运行级别

  • 模块级(setup_module/teardown_module)开始于模块始末,全局的

  • 函数级(setup_function/teardown_function)只对函数用例生效(不在类中)

  • 类级(setup_class/teardown_class)只在类中前后运行一次(在类中)

  • 方法级(setup_method/teardown_method)开始于方法始末(在类中)

  • 类里面的(setup/teardown)运行在调用方法的前后

1、模块级别和函数级别

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

函数级(setup_function/teardown_function)只对函数用例生效(不在类中)

代码示例:

import pytest
def setup_module():
    print("setup_module:整个.py模块只执行一次")
    print("比如:所有用例开始前只打开一次浏览器")
def teardown_module():
    print("teardown_module:整个.py模块只执行一次")
    print("比如:所有用例结束只最后关闭浏览器")
#  函数式,setup_function/teardown_function 每个用例开始和结束调用一次
def setup_function():
    print("setup_function:每个用例开始前都会执行")
def teardown_function():
    print("teardown_function:每个用例结束后都会执行")
def test_01():
    print("正在执行--test_01")
    a = "sure"
    assert e in a
def test_02():
    print("正在执行--test_02")
    a = "hello"
    assert e in a
    #  assert hasattr(a, ‘check‘)
def test_03():
    print("正在执行--test_03")
    a = "hello"
    b = "hello world"
    assert a in b
if __name__ == "_main_":
    pytest.main(["-s", "test_fixt.py"])
#  -s参数是为了显示用例的打印信息。 -q参数只显示结果,不显示过程
运行结果:

  test_fixt.py::test_01 setup_module:整个.py模块只执行一次
  比如:所有用例开始前只打开一次浏览器
  setup_function:每个用例开始前都会执行
  PASSED [ 33%]正在执行--test_01
  teardown_function:每个用例结束后都会执行

  test_fixt.py::test_02 setup_function:每个用例开始前都会执行
  PASSED [ 66%]正在执行--test_02
  teardown_function:每个用例结束后都会执行

  test_fixt.py::test_03 setup_function:每个用例开始前都会执行
  PASSED [100%]正在执行--test_03
  teardown_function:每个用例结束后都会执行
  teardown_module:整个.py模块只执行一次
  比如:所有用例结束只最后关闭浏览器

2、类和方法

类级(setup_class/teardown_class)只在类中前后运行一次(在类中)

类里面的(setup/teardown)运行在调用方法的前后

.setup/teardown和unittest里面的setup/teardown是一样的功能,setup_class和teardown_class等价于unittest里面的setupClass和teardownClass

示例代码:

#  类和方法
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("teardown_class:所有用例执行之后")
    def setup_method(self):
        print("setup_method:  所有用例开始前执行")
    def teardown_method(self):
        print("setup_method:  所有用例开始后执行")
    def test_one(self):
        print("正在执行----test_one")
        x = "this"
        assert h in x
    def test_two(self):
        print("正在执行----test_two")
        x = "hello"
        # assert hasattr(x, ‘check‘)
        assert o in x
    def test_three(self):
        print("正在执行----test_three")
        a = "hello"
        b = "hello world"
        assert a in b


if __name__ == "__main__":
    pytest.main(["-s", "test_fixtclass.py"])
运行结果:

  setup_class:所有用例执行之前
  setup_method: 所有用例开始前执行
  setup: 每个用例开始前执行
  PASSED [ 33%]正在执行----test_one
  teardown: 每个用例结束后执行
  setup_method: 所有用例开始后执行
  setup_method: 所有用例开始前执行
  setup: 每个用例开始前执行
  PASSED [ 66%]正在执行----test_two
  teardown: 每个用例结束后执行
  setup_method: 所有用例开始后执行
  setup_method: 所有用例开始前执行
  setup: 每个用例开始前执行
  PASSED [100%]正在执行----test_three
  teardown: 每个用例结束后执行
  setup_method: 所有用例开始后执行
  teardown_class:所有用例执行之后

运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
备注:这里setup_method和teardown_method的功能和setup/teardown功能是一样的,一般二者用其中一个即可

3、函数和类混合

import pytest
# 类和方法
def setup_module():
    print("setup_module:整个.py模块只执行一次")
    print("比如:所有用例开始前只打开一次浏览器")

def teardown_module():
    print("teardown_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_two")
    x = "hello"
    assert hasattr(x, check)

class TestCase():

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

    def teardown_class(self):
        print("teardown_class:所有用例执行之前")

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

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

if __name__ == "__main__":
    pytest.main(["-s", "test_fixtclass.py"])
运行结果:
  test_fixtclass.py setup_module:整个.py模块只执行一次
  比如:所有用例开始前只打开一次浏览器
  setup_function:每个用例开始前都会执行
  正在执行----test_one
  .teardown_function:每个用例结束前都会执行
  setup_function:每个用例开始前都会执行
  正在执行----test_two
  Fteardown_function:每个用例结束前都会执行
  setup_class:所有用例执行之前
  正在执行----test_three
  .正在执行----test_four
  Fteardown_class:所有用例执行之前
  teardown_module:整个.py模块只执行一次
  比如:所有用例结束只最后关闭浏览器

setup_module/teardown_module的优先级是最大的,

函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉

 

pytest第二天-用例的setup和teardown

原文:https://www.cnblogs.com/jialeliu/p/14132337.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!