首页 > 其他 > 详细

pytest入坑到放弃3--setup\teardown(前置\后置)

时间:2020-08-17 15:46:56      阅读:75      评论:0      收藏:0      [点我收藏+]

1、setup\teardown 运行级别

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

 

2、模块级别(setup_module/teardown_module) 和 函数级(setup_function/teardown_function)

# File  : test_demo_2.py
# IDE   : PyCharm

import pytest

# 整个文件只执行一次,在 selenium 中可以实现一个浏览器运行多个测试用例
def setup_module():
    print(* * 10 +用例开始,只运行一次!+ * * 10)

def teardown_module():
    print(* * 10 +用例结束,只运行一次! + * * 10)
# 每执行一个测试用例执行一次setup和teardown
def setup_function():
    print(* * 10 +用例开始!+ * * 10)

def teardown_function():
    print(* * 10 +用例结束! + * * 10)

def test_1():
    print(* * 5 + test_1 + * * 5)
    a = hello world!
    assert hello in a

def test_2():
    print(* * 5 + test_2 + * * 5)
    x = hello world!
    assert hasattr(x, helloWorld)

@pytest.mark.smoke
def test_3():
    print(* * 5 + test_3 + * * 5)
    b = 3
    assert b == 4

 

3、类级(setup_class/teardown_class):

  pytest 中的 setup() 和 teardown(),等价于unittest 框架中有 setUp() 和 tearDown()

  pytest 中的 setup_class() 和 teardown_class(),等价于 unittest 中的 setupClass() 和 teardownClass()

 

4、运行顺序:

  setup_module > setup_class > setup_method > setup > teardown > teardown_method > teardown_class > teardown_module 

 

def setup_module():
print(‘*‘ * 10 +用例开始!‘+ ‘*‘ * 10)

def teardown_module():
print(‘*‘ * 10 +用例结束!‘ + ‘*‘ * 10)

def test_1():
print(‘*‘ * 5 + ‘test_1‘ + ‘*‘ * 5)
a = ‘hello world!‘
assert ‘hello‘ in a

def test_2():
print(‘*‘ * 5 + ‘test_2‘ + ‘*‘ * 5)
x = ‘hello world!‘
assert hasattr(x, ‘helloWorld‘)

@pytest.mark.smoke
def test_3():
print(‘*‘ * 5 + ‘test_3‘ + ‘*‘ * 5)
b = 3
assert b == 4

pytest入坑到放弃3--setup\teardown(前置\后置)

原文:https://www.cnblogs.com/xiaohuboke/p/13517574.html

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