Pytest的前后置(固件、夹具)处理
有一些初始化配置和测试之后的收尾,只需要处理一次,这个时候我们就要用到夹具。
Pytest提供了以下几种setup和teardown方法:
setup_function
和teardown_function
: 用于每个测试函数setup_method
和teardown_method
: 用于每个测试方法(类级别)setup_module 和 teardown_module
: 用于每个模块setup_class 和 teardown_class
: 用于每个类setup
和teardown
: 用于每个测试用例(一般通过fixture实现)
"""
# 测试用例前后置固件setup和teardown使用
"""
def setup_module():print("module前置")
def teardown_module():print("module后置")# def setup_function():
# print("函数前置")
# def teardown_function():
# print("函数后置")
#
# def test_03():
# print("测试333")class TestSetupTeardown:def setup(self):print("所有用例前置,一般通过fixture实现")def setup_class(self):print("类前置")def setup_method(self):print("方法前置")def test_01(self):print("测试111")def test_02(self):print("测试222")def teardown_method(self):print("方法后置")def teardown_class(self):print("类后置")def teardown(self):print("所有用例前置,一般通过fixture实现")if __name__ == '__main__':pytest.main()
执行结果:
Python Pytest中setup和teardown使用全攻略
继承方式实现前后置
# setup_teardown.py文件"""封装setup、teardown前置后置"""
class SetupTeardown:def setup(self):print("所有用例前置,一般通过fixture实现")def setup_class(self):print("类前置")def setup_method(self):print("方法前置")def teardown_method(self):print("方法后置")def teardown_class(self):print("类后置")def teardown(self):print("所有用例前置,一般通过fixture实现")
# 测试用例.py文件"""类继承方式"""
from setup_teardown import SetupTeardown
class TestSetupTeardown(SetupTeardown):def test_01(self):print("测试111")def test_02(self):print("测试222")if __name__ == '__main__':pytest.main()
执行结果:
使用fixture装饰器来实现部分用例的前后置
import pytest@pytest.fixture(scope="function")
def function_fixture():print("函数级fixture setup")yieldprint("函数级fixture teardown")@pytest.fixture(scope="class")
def class_fixture():print("类级fixture setup")yieldprint("类级fixture teardown")@pytest.fixture(scope="module")
def module_fixture():print("模块级fixture setup")yieldprint("模块级fixture teardown")@pytest.fixture(scope="session")
def session_fixture():print("会话级fixture setup")yieldprint("会话级fixture teardown")def test_one(function_fixture):print("执行test_one")assert Trueclass TestClass:def test_two(self, class_fixture):print("执行test_two")assert True
Pytest之Fixture前后置处理详解
前置后置实际使用场景
- 数据库连接和关闭
- 文件打开和关闭
- 用例前置和后置步骤、数据处理
# test_database.py
import pytest
import sqlite3@pytest.fixture(scope="module")
def db_connection():print("创建数据库连接")conn = sqlite3.connect(":memory:")yield connprint("关闭数据库连接")conn.close()@pytest.fixture(scope="function")
def db_cursor(db_connection):cursor = db_connection.cursor()print("创建表")cursor.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)")yield cursorprint("删除表")cursor.execute("DROP TABLE test")def test_insert(db_cursor):db_cursor.execute("INSERT INTO test (name) VALUES ('Alice')")db_cursor.execute("SELECT * FROM test")result = db_cursor.fetchall()assert len(result) == 1assert result[0][1] == 'Alice'def test_delete(db_cursor):db_cursor.execute("INSERT INTO test (name) VALUES ('Bob')")db_cursor.execute("DELETE FROM test WHERE name='Bob'")db_cursor.execute("SELECT * FROM test")result = db_cursor.fetchall()assert len(result) == 0
执行结果: