Skip to main content
 首页 » 编程设计

python之无法使用 mock_open() 在模拟函数中测试全局值

2024年11月24日8JustinYoung

我正在尝试使用 mock_open() 来测试我的应用程序 Mocking file reads using patch decorator and side_effect ,但我无法检查 under 测试中的全局值集。它仅在函数返回值时有效。

我的应用程序代码片段看起来像

#!/usr/bin/env python 
 # -*- coding: utf-8 -*- 
 
thresh_dict = {} 
allowed_san_dict = {} 
max_eval_count = "" 
clear_eval_count = "" 
 
def generate_cfg_thresh_map(): 
    """ For generating a mapping table containing the thresholds for each of the 
    production devices. The file is read one line at a time and split on whitespace 
    to get the individual fields 
    """ 
 
    global allowed_san_dict 
    global max_eval_count 
    global clear_eval_count 
    try: 
        with open ('/opt/config/pwr_thresh_cfg', 'r') as fh: 
            for line in fh: 
                split_line = line.split() 
                if line.startswith("ENTRY_THRESH_COUNT"): 
                    max_eval_count = int(split_line[1]) 
                elif line.startswith("EXIT_THRESH_COUNT"): 
                    clear_eval_count = int(split_line[1]) 
                else: 
                    thresh_dict[split_line[0]]  = [int(split_line[1]), int(split_line[2]), int(split_line[3])] 
                    allowed_san_dict[split_line[0]] = int(split_line[4]) 
 
    except Exception as error: 
        log_to_journal('read failed from pwr_thresh_cfg') 

test_app.py 文件看起来像

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
 
# Include require py.test libraries for the suite to run 
import unittest 
import pytest 
import mock 
 
import app 
 
from mock import patch, mock_open 
 
class ReportMonitor(unittest.TestCase): 
    def test_generate_cfg_thresh_map(self): 
        with patch('app.open', mock_open(read_data='ENTRY_THRESH_COUNT 200')) as _: 
            app.generate_cfg_thresh_map() 
            assert app.max_eval_count == 200 
            print('value for %s' % (app.max_eval_count)) 
            print('value for %s' % (app.generate_cfg_thresh_map())) 

问题是我想断言 generate_cfg_thresh_map() 函数中设置的 max_eval_count 的值。由于该值是在全局变量中设置的,而不是从函数返回的,所以我无法在我的测试用例中断言该值。我不能从函数中使用 return,即根本不更改应用程序代码。

我该怎么做?即运行被测函数并检查函数为测试字符串设置的值,并根据函数设置的值断言单元测试用例。

请您参考如下方法:

引用 mock_open docs :

read_data is a string for the read(), readline(), and readlines() methods of the file handle to return. Calls to those methods will take data from read_data until it is depleted.

这意味着 read_data 将不会在迭代模拟文件对象时被调用。一个简单的解决方法是将 generate_cfg_thresh_map 函数的实现更改为:

def generate_cfg_thresh_map(): 
    ... 
    try: 
        with open ('/opt/config/pwr_thresh_cfg', 'r') as fh: 
            <b>for line in fh.readlines():</b> 
                ... 

如果您不能更改函数,或者您可以增强 mock_open 返回的模拟对象:

python 3

class ReportMonitor(unittest.TestCase): 
    def test_generate_cfg_thresh_map(self): 
        m = mock_open(read_data='ENTRY_THRESH_COUNT 200') 
        m.return_value.__iter__ = lambda self: self 
        m.return_value.__next__ = lambda self: next(iter(self.readline, '')) 
        with patch('app.open', m): 
            app.generate_cfg_thresh_map() 
            assert app.max_eval_count == 200 

python 2

class ReportMonitor(unittest.TestCase): 
    def test_generate_cfg_thresh_map(self): 
        m = mock_open(read_data='ENTRY_THRESH_COUNT 200') 
        m.return_value.__iter__ = lambda self: iter(self.readline, '') 
        with patch('app.open', m): 
            app.generate_cfg_thresh_map() 
            assert app.max_eval_count == 200 

致谢:Customizing unittest.mock.mock_open for iteration .