使用pytest时在装饰器中模拟对象

问题描述 投票:1回答:1

在下面的示例中,我有一个装饰器。在decorator中,我实例化一个DB连接类。我在下面有一个测试类,我想在装饰器中模拟数据库连接类。我该怎么办?

# importing libraries 
import time 
import math 

# decorator to calculate duration 
# taken by any function. 
def calculate_time(func): 

    # added arguments inside the inner1, 
    # if function takes any arguments, 
    # can be added like this. 
    def inner1(*args, **kwargs): 

        db_conn = DBConn()
        # storing time before function execution 
        begin = time.time() 

        func(*args, **kwargs) 

        # storing time after function execution 
        end = time.time() 
        db_conn.logTime(begin, end)
        print("Total time taken in : ", func.__name__, end - begin) 

    return inner1 



# this can be added to any function present, 
# in this case to calculate a factorial 
@calculate_time
def test_factorial(num): 

    # sleep 2 seconds because it takes very less time 
    # so that you can see the actual difference 
    time.sleep(2) 
    print(math.factorial(num)) 

# calling the function. 
factorial(10) 
python python-3.x pytest python-unittest python-decorators
1个回答
0
投票

确定,请更清楚:如果您的模块位于包mypackage中,并且带有装饰器的模块calculate_time.py看起来像:

from mypackage.dbconn import DBConn

def calculate_time(func):
    def inner1(*args, **kwargs):
        ...

并且您的模块factorial.py具有:

from mypackage.calculate_time import calculate_time

@calculate_time
def factorial(num):
    time.sleep(2)
    print(math.factorial(num))

然后您的测试可能如下所示:

from unittest.mock import patch    
from mypackage.factorial import factorial

class FakeConn:
    def logTime(self, begin, end):
        print(begin, end)

@patch('mypackage.calculate_time.DBConn', new=FakeConn)
def test_factorial():
    print(factorial(10))
© www.soinside.com 2019 - 2024. All rights reserved.