Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/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为测试注入模拟值_Python_Unit Testing_Flask - Fatal编程技术网

Python为测试注入模拟值

Python为测试注入模拟值,python,unit-testing,flask,Python,Unit Testing,Flask,我有一种情况,在Flask web应用程序中,我有一个从我想要进行单元测试的处理程序向下几层的函数。该函数接受一些参数,并根据从数据库中读取的不同值进行更改。大致如下: def func_to_test(a=1, b=10, c=100): # do some processing if (read_d_from_database() == 10): # override a to something else # do something

我有一种情况,在Flask web应用程序中,我有一个从我想要进行单元测试的处理程序向下几层的函数。该函数接受一些参数,并根据从数据库中读取的不同值进行更改。大致如下:

  def func_to_test(a=1, b=10, c=100):
     # do some processing
     if (read_d_from_database() == 10):
        # override a to something else
        # do something
     if (read_X_from_database() == 45):
        # override b to 45
        # do something
     #etc..
如何从外部以独立的方式测试此函数,以便模拟每次返回不同值的read_d_from_database()等?

您可以查看。它允许您重写某些方法的行为

>>> @patch('__main__.SomeClass')
... def function(normal_argument, mock_class):
...     print(mock_class is SomeClass)
...
>>> function(None)
True