Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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 unittest中断言执行顺序_Python_Unit Testing_Mocking_Python Unittest - Fatal编程技术网

在python unittest中断言执行顺序

在python unittest中断言执行顺序,python,unit-testing,mocking,python-unittest,Python,Unit Testing,Mocking,Python Unittest,我有一个函数,它创建一个临时目录,切换到该临时目录,执行一些工作,然后切换回原始目录。我正在尝试编写一个单元测试来测试这一点。我在验证当前目录是否已更改为temp dir并再次更改为temp dir时没有问题,但在验证重要内容是否发生在这些调用之间时遇到了问题 我最初的想法是将函数抽象为三个子函数,以便测试调用顺序。我可以用mock替换这三个子函数中的每一个,以验证它们是否被调用——但是,我仍然面临着验证顺序的问题。在模拟中,我可以使用assert\u has\u调用,但在什么对象上调用该函数呢

我有一个函数,它创建一个临时目录,切换到该临时目录,执行一些工作,然后切换回原始目录。我正在尝试编写一个单元测试来测试这一点。我在验证当前目录是否已更改为temp dir并再次更改为temp dir时没有问题,但在验证重要内容是否发生在这些调用之间时遇到了问题

我最初的想法是将函数抽象为三个子函数,以便测试调用顺序。我可以用mock替换这三个子函数中的每一个,以验证它们是否被调用——但是,我仍然面临着验证顺序的问题。在模拟中,我可以使用assert\u has\u调用,但在什么对象上调用该函数呢

下面是我要测试的类:

import shutil
import os
import subprocess
import tempfile
import pkg_resources


class Converter:
    def __init__(self, encoded_file_path):
        self.encoded_file_path = encoded_file_path
        self.unencoded_file_path = None
        self.original_path = None
        self.temp_dir = None

    def change_to_temp_dir(self):
        self.original_path = os.getcwd()
        self.temp_dir = tempfile.mkdtemp()
        os.chdir(self.temp_dir)

    def change_to_original_dir(self):
        os.chdir(self.original_path)
        shutil.rmtree(self.temp_dir)

    def do_stuff(self):
        pass

    def run(self):
        self.change_to_temp_dir()
        self.do_stuff()
        self.change_to_original_dir()
这就是我在编写测试用例时所做的:

def test_converter(self, pkg_resources, tempfile, subprocess, os, shutil):
    encoded_file_path = Mock()

    converter = Converter(encoded_file_path)
    converter.change_to_temp_dir = Mock()
    converter.do_stuff= Mock()
    converter.change_to_original_dir = Mock()

    assert converter.encoded_file_path == encoded_file_path
    assert converter.unencoded_file_path is None

    converter.run()

现在我已经模拟了每个函数,我可以验证它们是否被调用,但不能验证调用顺序。如何执行此操作?

一种解决方法是创建一个单独的模拟对象,将方法附加到它,并使用它检查调用顺序:

converter = Converter(encoded_file_path)
converter.change_to_temp_dir = Mock()
converter.do_stuff = Mock()
converter.change_to_original_dir = Mock()

m = Mock()
m.configure_mock(first=converter.change_to_temp_dir,
                 second=converter.do_stuff,
                 third=converter.change_to_original_dir)

converter.run()
m.assert_has_calls([call.first(), call.second(), call.third()])

这不起作用,这些方法仍然绑定到转换器,并且对mock_存根没有任何影响。然而,它确实给了我一个想法。如果你要撤销任务呢?与其将转换器函数添加到mock_存根,不如用mock_存根函数替换转换器函数?@SeanPrice在
converter
方法上放置mock后,检查是否实例化了
m
mock。啊,我明白了。你的答案很好,谢谢!