0

Background: I am trying to test my wrapper class for git. This class does things like retry git commands when they hang or fail (Our repos are huge). I can mock git with something like mockproc - https://pypi.python.org/pypi/MockProc/

The problem I am facing is how to test retry logic, Ideally what I want is the ability to do this -

@integrationtest('git')
def test_git_fetch(mock_proc):
  mock_proc.add_scripts("sleep (60)",
                        "sleep (60)",
                        "exit (-1)" )
  mygit.fetch()
  assert mock_proc.output_signal_count == 3
  assert mock_proc.get_output_signals[0] == -1

So in this case, when mygit.fetch() retries git 3 times, each time it runs the scripts defined in add_scripts function. With mockproc module I can mock any command for a given script, but I am stumped about how to achieve the retry testing such that each retry runs a different script. I would be grateful if someone can point to some module which can help me achieving this or some way to extend mockproc to add retry handling.

ocwirk
  • 1,079
  • 1
  • 15
  • 35

1 Answers1

1

I encounter the similar issue before when I do mock test. This answer I followed and it is very helpful

is there a way to track the number of times a function is called?

One approach is to create a proxy of the instance for which you want to count attribute access:

from collections import Counter

class CountingProxy():
    def __init__(self, instance):
        self._instance = instance
        self.count = Counter()

    def __getattr__(self, key):
        if hasattr(self._instance, key):
            self.count[key] += 1
        return getattr(self._instance, key)


>>> l = [1,2,3,4,5]
>>> cl = CountingProxy(l)
>>> cl.pop()
5
>>> cl.append(10)
>>> cl.index(3)
2
>>> cl.reverse()
>>> cl.reverse()
>>> cl.count
Counter({'reverse': 2, 'pop': 1, 'append': 1, 'index': 1})
Community
  • 1
  • 1
zzhang
  • 69
  • 4