Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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/7/python-2.7/5.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/7/user-interface/2.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_Python 2.7_Unit Testing_Testing_Python Unittest - Fatal编程技术网

如何在Python中对变量重新分配进行单元测试

如何在Python中对变量重新分配进行单元测试,python,python-2.7,unit-testing,testing,python-unittest,Python,Python 2.7,Unit Testing,Testing,Python Unittest,主要问题:什么是单元测试类变量self.file_path是否由给定以下类的方法成功重新分配的最有效方法 class FileHandler(object): def __init__(self, file_path): self.file_path = file_path # /original/path/file.csv def reassign_path(new_path): self.file_path = new_path

主要问题:什么是单元测试类变量self.file_path是否由给定以下类的方法成功重新分配的最有效方法

class FileHandler(object):
    def __init__(self, file_path):
        self.file_path = file_path     # /original/path/file.csv

    def reassign_path(new_path):
        self.file_path = new_path      # /new/path/file.csv
        self.run_long_operation(self.file_path)
我最初尝试在运行重新分配路径后测试self.file_path变量是否不同,但该方法需要很长时间才能完成,并且大大降低了测试速度


次要问题:有没有一种方法可以简单地检查一个方法是否在不运行整个方法的情况下重新分配一个类变量?有更好的方法来解决这个问题吗?

我不是说它是最好的解决方案,但它是测试友好的。它类似于模拟缓慢的操作,但更显式,并使用其定义的接口测试函数,而不是对要模拟的内容进行特定于实现的假设

与其硬编码长而慢的操作,不如将其作为重新分配路径的参数

现在,您可以通过传递另一个更快的函数作为第二个参数来快速测试重新分配路径:

def test_reassign_path_quickly(self):
     fh = FileHandler('/old/path')
     fh.reassign_path('/new/path', lambda *args: None)
     self.assertEquals(fh.file_path, '/new/path')

理论上,您可以使用内置的ast模块分析代码的抽象语法树来检查这一点。这不会让你的任何代码运行。你可以模拟长时间的operation@JETM-你能详细介绍一下我是如何嘲笑这项漫长的行动的吗?对不起,我对模拟和单元测试还很陌生。@chepner的回答比我说的要好。
def test_reassign_path_quickly(self):
     fh = FileHandler('/old/path')
     fh.reassign_path('/new/path', lambda *args: None)
     self.assertEquals(fh.file_path, '/new/path')