Kuratorn
Kuratorn

Reputation: 305

Python: Mocking a local gitpython repository

I am currently working with a project where I am using gitpython to check a few things in git repositories. I have now started to write tests for my project, and by doing so, I have realized I need to mock a few things.

In this project I am making git.Repo classes by cloning repositories as well as using local repositories. I could run these tests locally, on my computer, but it will not be possible to assume that the tests will work on other computers.

Essentially, the question is, how do I mock repositories in gitpython? How can I "pretend" that a repository exists on a specified path on the current computer?

You can see what needs to be mocked below:

import git
repository = git.Repo('./local_repo_path')

Upvotes: 5

Views: 4825

Answers (1)

Andrew Bowman
Andrew Bowman

Reputation: 869

based on the code in https://stackoverflow.com/a/32428653/2903486

I was able to create a mock in Python 3:

from unittest.mock import patch
@patch("git.Repo")
@patch("git.Git")
def test_stash_pull(mock_git, mock_repo):
    p = mock_git.return_value = False
    type(mock_repo.clone_from.return_value).bare = p
    # your git.Repo call here

Upvotes: 4

Related Questions