Pytest框架-fixtrue前置后置

Pytest-fixtrue前置后置

基本的前后置

def setup(self) #case开始前执行

def teardown(self) #case结算后行

def setup_class(self) #每个类前执行

def teardown_class(self) #每个类后执行

fixtrue装饰器–部分前后置

fixtrue装饰器

@pytes.fixtrue(scope='作用域',params='数据驱动',autouse='自动执行',
               ids='数据驱动时重命名参数名',name='给fixtrue作用的函数重命名')

scope参数:

  1. function 函数之前/后执行
  2. class 类之前/后执行
  3. module 模块之前/后执行 (不太常用)
  4. package/session 回话前/后执行

--------函数前后置--------------------
test.py文件

@pytest.fixtrue(scope='function') #标记execute_database_sql函数为固件
def execute_database_sql():
    print('sql') #前置代码
    yieid
    print('sql2')#后置代码
        
class Testfixtrue:
    
    def test_test1(self):
        print('test1')
        
    def test_test2(self,execute_database_sql #传入固件进行使用):
        print('test2')

-------类前后置--------------------
test.py文件

@pytest.fixtrue(scope='class')
def execute_database_sql():
    print('sql')
    yieid
    print('sql2')
                   
 class Test01:
     def test_test1(self):
        print('test1')
                   
 @pytest.mack.usrfixtures('execute_database_sql')   
 #添加mack只执行这个类                
 class Test02:
     def test_test2(self):
        print('test2') 
                   

--------会话前后置 --------------------
注:

  1. 一般会结合conftest.py文件一起使用;
  2. 在conftest.py 单独存放fixtrue固件的配置文件;
  3. 可以存在多个conftest.py文件。

conftest.py文件

@pytest.fixtrue(scope='session',autouse='true')
#autouse自动执行不加的话test文件中的函数无法执行fixtrue
def execute_database_sql():
  print('sql')
    yieid
  print('sql2')  

test.py文件

class Testfixtrue:    
  def test_test1(self):
    print('test1')        
  def test_test2(self):
    print('test2')

支付宝红包外卖加鸡腿😊在这里插入图片描述

### pytest Framework Setup and Teardown Methods In the context of `pytest`, setup and teardown methods are used to define actions that should be executed before or after tests. These mechanisms ensure that each test runs in an isolated environment, preventing side effects from one test affecting others. #### Using Fixtures for Setup and Teardown `pytest` provides fixtures as a powerful mechanism for managing setup and teardown operations. A fixture is defined using the `@pytest.fixture` decorator, and it can perform initialization (setup) when requested by a test function and cleanup (teardown) once the test completes[^1]. Here is an example demonstrating how to use fixtures: ```python import pytest @pytest.fixture def resource_setup(): print("\nSetup called.") yield "Resource" print("\nTeardown called.") def test_example(resource_setup): print(f"\nTest running with {resource_setup}.") assert True ``` The above code defines a fixture named `resource_setup`. The `yield` statement separates the setup phase (before yielding) from the teardown phase (after yielding). When the test concludes, the teardown logic executes automatically[^2]. #### Class-Level Setup and Teardown For scenarios where you need class-level setup and teardown, you can combine fixtures with scope parameters. By setting the `scope="class"` argument within the `@pytest.fixture` decorator, the same fixture instance will persist across all methods inside a single test class[^3]: ```python import pytest @pytest.fixture(scope="class") def class_resource_setup(request): request.cls.resource = "Class Resource" print("\nClass level setup.") yield print("\nClass level teardown.") @pytest.mark.usefixtures("class_resource_setup") class TestExample: def test_one(self): print(f"\nTest One uses {self.resource}.") assert self.resource == "Class Resource" def test_two(self): print(f"\nTest Two also uses {self.resource}.") assert self.resource == "Class Resource" ``` This ensures both `test_one` and `test_two` share the same setup while executing their respective assertions independently[^4]. #### Module and Session Scopes Fixtures support broader scopes such as module (`scope="module"`) or session (`scope="session"`), which execute only once per module or entire test suite respectively. This reduces redundant setups during large-scale testing sessions but requires careful handling due to shared states between multiple tests[^3]. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值