python - py.test -- 模拟常量并在测试函数中引发异常

标签 python testing mocking pytest

我正在使用 py.test 和 mock。我一直无法模拟一个常量。我的测试修改了分配给常量的字典值。这应该在我的测试中引发异常,但到目前为止它没有。我不确定问题出在哪里,如果能帮助查明问题,我将不胜感激。谢谢。

the_module.py

MY_DICT = {'one': 1, 'two': 2, 'three': 3}                                        

class OneMissingException(Exception):                                             
    pass                                                                          

class Test1(object):                                                              
    def __init__(self):                                                           
        self.mydict = MY_DICT                                                     

    @property                                                                     
    def mydict(self):                                                             
        return self._mydict                                                       

    @mydict.setter                                                                
    def mydict(self, mydict):                                                     
        if 'one' not in mydict:                                                   
            raise OneMissingException                                             
        self._mydict = mydict 

test_themodule.py

import pytest                                                                                                                                                  
from unittest import mock                                                      
from the_module import Test1, OneMissingException                              

@pytest.fixture(scope='function')                                              
def my_dict():                                                                 
    return {'one': 1, 'two': 2, 'three': 3}                                    

def test_verify_test1_exception(my_dict):                                      
    my_dict.pop('one') # comment this out and test still passes                                                       
    with mock.patch("the_module.MY_DICT") as mydict:                           
        mydict.return_value.return_value = my_dict                             
        with pytest.raises(OneMissingException):                               
            Test1()  

最佳答案

在你的情况下你不需要模拟(你试图以错误的方式使用它,因为没有人调用 MY_DICT 而你尝试了 return_value)

只使用 pytest 的 monkeypatch fixture:

import pytest                                                                                                                                                  
from unittest import mock                                                      
from the_module import Test1, OneMissingException                              

@pytest.fixture                                              
def patched_my_dict(monkeypatch):                                                                 
    patched = {'one': 1, 'two': 2, 'three': 3}
    monkeypatch.setattr("the_module.MY_DICT", patched)
    return patched                                    

def test_verify_test1_exception(patched_my_dict):                                      
    patched_my_dict.pop('one') # comment this out and test will not pass                                                       
    with pytest.raises(OneMissingException):                               
        Test1()  

关于python - py.test -- 模拟常量并在测试函数中引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29446175/

相关文章:

java - 如何修复 java.lang.IllegalArgumentException : bound must be positive

python - 使用 pytest-mock 检查调用顺序

javascript - Django - 查询集索引而不是循环

python - 使用 fnmatch 查找具有名称模式的所有文件夹

android - 由于 Mockito.doThrow,测试永远不会执行

Facebook 测试用户和授权

python - ValueError : Cannot feed value of shape () for Tensor 'input_example_tensor:0' , 其形状为 '(?,)'

python - 向合并的数据帧添加子索引

unit-testing - Groovy MockFor : How to avoid nested closures when using multiple mocks?

unit-testing - 如何使用Spock在Grails中模拟同一个类的多个方法?