Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python py.test——模拟常量并在测试函数中引发异常_Python_Testing_Mocking_Pytest - Fatal编程技术网

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

Python py.test——模拟常量并在测试函数中引发异常,python,testing,mocking,pytest,Python,Testing,Mocking,Pytest,我正在使用py.test和mock。我无法模仿一个常数。我的测试修改分配给常量的dict值。这应该会在我的测试中引发一个异常,但到目前为止还没有。我不确定问题出在哪里,如果能帮我找出问题所在,我将不胜感激。多谢各位 _模块.py MY_DICT = {'one': 1, 'two': 2, 'three': 3} class OneMissingException(Exception):

我正在使用py.test和mock。我无法模仿一个常数。我的测试修改分配给常量的dict值。这应该会在我的测试中引发一个异常,但到目前为止还没有。我不确定问题出在哪里,如果能帮我找出问题所在,我将不胜感激。多谢各位

_模块.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 
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()  
测试模块.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 
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()  

在您的情况下,您不需要mock(并且您试图以错误的方式使用它,因为没有人调用MY_DICT,您尝试返回_value)

仅使用pytest的monkeypatch夹具:

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()  

谢谢你澄清这一点。不过,我不清楚你的解释。您能帮助我理解我是如何错误地修补属性(常量)的吗?