Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:27:10+00:00 2026-06-11T18:27:10+00:00

What would be the simplest way to unit-test run function? Here I’m not interested

  • 0

What would be the simplest way to unit-test run function? Here I’m not interested in what fun* functions do, but only if the order of their invocation is correct.

from mock import Mock

(many, arguments, four, fun) = ('many', 'arguments', 'four', 'fun') 

class TemplateMethod(object):

    def fun1(self, many, parameters, go, here):
        raise NotImplementedError()

    @classmethod
    def fun2(cls, many, parameters, go, here):
        pass

    @staticmethod
    def fun3(many, parameters, go, here):
        pass

    def run(self):
        result1 = self.fun1(many, arguments, four, fun)
        result1.do_something()

        self.fun2(many, arguments, four, fun)
        self.fun3(many, arguments, four, fun)

The only requirement for the solution is non-intrusiveness towards the class under test.

SOLUTION:

This is somewhat of a draft, actually, and this simple class could be fixed and extended in so many ways that I don’t care even to think about it. The point is that you can now simply record all the invocations of all the methods in a template method. You can also specify a list of object that shouldn’t be mocked by the class (i.e. the function you are recording invocations for).

Special thanks go to @Damian Schenkelman, who gave me some important pointers.

class MockingInvocationRecorder(object):

    FUNCTION_TYPES = ('function', 'classmethod', 'staticmethod')

    def __init__(self, obj, dont_mock_list):
        self._invocation_list = []

        name_list = [exc.__name__ for exc in dont_mock_list]
        self._wrap_memfuns(obj, name_list)

    @property
    def invocations(self):
        return tuple(self._invocation_list)

    def _wrap_single(self, memfun, exclude_list):
        def wrapper(*args, **kwargs):
            self._invocation_list.append(memfun.__name__)

            if memfun.__name__ in exclude_list:
                return memfun(*args, **kwargs)
            else:
                return Mock()

        return wrapper

    def _wrap_memfuns(self, obj, exclude_list):
        for (mem_name, mem) in type(obj).__dict__.iteritems():
            if type(mem).__name__ in self.FUNCTION_TYPES:
                wrapper = self._wrap_single(getattr(obj, mem_name), exclude_list)
                setattr(obj, mem_name, wrapper)

You can now test invocation order by something like this:

tm = TemplateMethod()
ir = MockingInvocationRecorder(tm, [tm.run])

tm.run()

print ir.invocations => ('run', 'fun1', 'fun2', 'fun3')

You can reference a member by class name:

ir = MockingInvocationRecorder(tm, [TemplateMethod.run])

Just be careful that you enlist all the methods that shouldn’t be mocked away.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:27:11+00:00Added an answer on June 11, 2026 at 6:27 pm

    Monkeypatch fun1, fun2, fun3 functions and use a list to keep track of the call order. After that, assert on the list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think the simplest way would be (!$var and $var!=0) , but is there
I am ideally after the simplest way to unit test my javascript via my
Would the simplest way to create a threadpool (or, at least obtain the code
What is the simplest way to customize the content display of ComboBoxItem? I would
What would be the best way (ideally, simplest) to convert an int to a
I have a string as follows CompilationStatistics_Compilation_StepList_Map_TimingUsage_ClockList_Clock_MinimumPeriod What would be the simplest way to
Function HEX is one of the core functions in sqlite I would like to
what would be the simplest way to change every nvarchar column in a database
What would be the simplest way to check if a remote SSH rsync fails
What would be the simplest way of protecting a directory in asp.net mvc? Currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.