如何编写GitPython克隆/拉函数的单元测试? [英] How to write unit tests for GitPython clone/pull functions?

查看:87
本文介绍了如何编写GitPython克隆/拉函数的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python项目,该项目正在使用GitPython对远程Git存储库执行克隆和提取功能.

I have a python project that is using GitPython to perform clone and pull functions against a remote Git repository.

作为一个简单的例子:

import git
from git import Git
from git import Repo


def clone_and_checkout(full_dir, git_url, repo_ver):

    repo = Repo.clone_from(
        url=git_url,
        to_path=full_dir
    )

    # Trigger re-create if repository is bare
    if repo.bare:
        raise git.exc.InvalidGitRepositoryError

    # Set origin and pull
    origin = repo.remotes.origin
    origin.pull()

    # Check out desired version of repository
    g = Git(full_dir)
    g.checkout(repo_ver)

我希望能够为此功能编写一个单元测试,但是显然,这需要扩展到目前的外部系统.

I want to be able to write a unit test for this function, but obviously this needs to reach out to an external system as it stands currently.

我很好奇是否有人有过像使用Mock模拟HTTP调用那样的方式来模拟这种外部交互的经验.我希望能够以一种可以在测试时模拟的方式执行这些任务,而无需调用实际的Git遥控器.

I am curious if anyone has experience mocking up this external interaction, in a manner similar to using Mock to mock up HTTP calls. I'd like to be able to perform these tasks in a way that can be mocked at test time without needing to call an actual Git remote.

我应该如何为此编写测试?

How should I go about writing tests for this?

编辑:为了更加清楚我的要求,我应该提一下我是Mock的新手,并且正在努力了解如何模拟这些类的实例,而不是类本身.我的问题应该用更好的措词来表达-类似于我如何使用Mock设置实例特有的属性(例如,裸)?"

EDIT: To be clearer about what I'm asking, I should mention I'm new to Mock and was struggling to understand how to Mock instances of these classes rather than the classes themselves. My question should have been phrased better - something along the lines of "how do I use Mock to set instance-specific properties like bare?"

此后,我对Mock有了很多了解,并且已经找到解决方法,因此,我将为自己的问题提供答案.

I have since learned much about Mock and have figured out how to do this, so I will provide an answer to my own question.

推荐答案

这似乎是对Mock的不完全了解或使用Patch方法的常见结果.

This seems to be a common result of an incomplete understanding of Mock, or the use of the Patch method.

要做的第一件事是阅读"其中补丁"部分中找到.有了这些信息,您应该能够使用patch函数模拟上述函数中使用的GitPython对象.这些装饰器将出现在单元测试功能的上方.

The first thing to do is read the "where to patch" section located on the Mock documentation. Armed with that information, you should be able to use the patch function to mock the GitPython objects used in the function above. These decorators would appear above your unit test function.

@mock.patch('gitter.Repo')
@mock.patch('gitter.Git')

为了提供这些模拟对象之一的实例的返回值,可以使用

In order to provide a return value for an instance of one of these mocked objects, you can use PropertyMock. Here's a full example of a unit test which leverages this:

import gitter  # file containing our clone function
import mock
import unittest


class test_gitter(unittest.TestCase):

    @mock.patch('gitter.Repo')
    @mock.patch('gitter.Git')
    def runTest(self, mock_git, mock_repo):

        # Set the "bare" attribute of the Repo instance to be False
        p = mock.PropertyMock(return_value=False)
        type(mock_repo.clone_from.return_value).bare = p

        gitter.clone_and_checkout(
            '/tmp/docker',
            'git@github.com:docker/docker.git',
            'master'
        )
        mock_git.checkout.called_once_with('master')

这篇关于如何编写GitPython克隆/拉函数的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆