简体   繁体   English

在python unittest中声明执行顺序

[英]Asserting execution order in python unittest

I have a function that creates a temporary directory, switches to that temporary directory, performs some work, and then switches back to the original directory. 我有一个函数可以创建一个临时目录,切换到该临时目录,执行一些工作,然后再切换回原始目录。 I am trying to write a unit test that tests this. 我正在尝试编写一个对此进行测试的单元测试。 I don't have a problem verifying that the current directory was changed to the temp dir and changed back again, but I'm having a problem verifying that the important stuff took place in between those calls. 验证当前目录是否已更改为temp dir并再次更改回目录时,我没有问题,但是验证这些调用之间是否发生了重要的事情时,我遇到了问题。

My original idea was to abstract the function into three sub functions so that I could test the call order. 我最初的想法是将函数抽象为三个子函数,以便测试调用顺序。 I can replace each of the three sub functions with mocks to verify that they are called -- however, I am still presented with the issue of verifying the order. 我可以用模拟替换三个子函数中的每一个,以验证它们是否被调用了-但是,我仍然遇到验证顺序的问题。 On a mock I can use assert_has_calls, but upon what object do I call that function? 在模拟中,我可以使用assert_has_calls,但是在哪个对象上调用该函数?

Here is the class I'm trying to test: 这是我要测试的课程:

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

This is as far as I got writing the test case: 就我编写测试用例而言:

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

Now that I have each function mocked, I can verify THAT they were called, but not in what ORDER. 现在,我已经模拟了每个函数,可以验证它们是否被调用,但不能以什么ORDER进行验证。 How do I go about doing this? 我该怎么做呢?

One workaround would to be to create a separate mock object, attach methods to it and use assert_has_calls() to check the call order: 一种解决方法是创建一个单独的模拟对象,将方法附加到该对象上,然后使用assert_has_calls()检查调用顺序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM