skip to navigation
skip to content

Not Logged In

redef 1.1

Test utility for redefining functions

Latest Version: 1.5

# Redef

To install, just run:

python build
python install

See [``](../blob/readme-tweaks/ under your install path for examples.

Redef is intended to create lexically scoped variables, that when destroyed, undo mock behavior.
It was inspired by a Perl module, [Test::Resub](

The best examples use `unittest`, but it should work with any testing framework.

import unittest
from redef import redef

class MyClass:
    def lame_function(self):
        return "something I don't want"

class MyTest(unittest.TestCase):
    def test1(self):
        myobj = MyClass()
        self.assertEqual(myobj.lame_function(), "something I don't want")
        want = 'something I want'
        rd_lf = redef(MyClass, 'lame_function', lambda s: want)
        self.assertEqual(myobj.lame_function(), want)

        # after test1, rd_lf gets deleted and resets

    def test2(self):
        myobj = MyClass()
        # test2 is uneffected by test1
        self.assertEqual(myobj.lame_function(), "something I don't want")

This doesn't have to be a function, you can also redefine attributes directly on an object.

class MyClass:
      unpredictable = 'random string'

my_global_object = MyClass()

class MyTest(unittest.TestCase):
     def test3(self):
         rd_u = redef(my_global_object, 'unpredictable', 'unit testable string')
         # ... test something awesome!
         self.assertEqual(my_global_object.unpredictable, 'unit testable string')

     def test4(self):
         # hey, my_global_object is back to being unpredictable
         self.assertEqual(my_global_object.unpredictable, 'unpredictable')

There are other useful functions provided on the redef object itself:

* Class Redef:

    * `called()`:
        Stores how many times a redef'd function was called.
    * `method_args()`:
        Stores the most recent `*args` to the redef'd function.
    * `named_method_args()`:
        Stores the most recent `**kwargs` to the redef'd function.
    * `reset()`:
        Sets `called`, `method_args`, and `named_method_args` back to the default state of `0, None, None`

`Redef` also provides a freebie static function:

* `redef(obj, key, value)`:
    Static constructor of a `Redef` object

  * `obj`: is a Class, Module, or Object you want to temporariliy change
  * `key`: The string name of the attribute you want to change
  * `value`: The new value.  If the value is None, you only capture called, method_args, and named_method_args
   * The None case won't redefine the key on the obj

These static functions were provided to show the usefulness of redef:
For example, you could capture stdout of a function call, and after capturing it,
`sys.stdout` goes back to normal:

* Class WriteCapturer:
    * Has 2 variables you want: `output`, `returned`

* Static Functions that return a WriteCapturer:
    * `stdout_of(func, *args, **kwargs)`:
        Call a function and capture the stdout.
        Returns a `WriteCapturer` object that has the stdout and the return value of calling func.

    * `stderr_of(func, *args, **kwargs)`:
        Call a function and capture the stderr.
        Returns a `WriteCapturer` object that has the stderr and the return value of calling func.
File Type Py Version Uploaded on Size
redef-1.1.tar.gz (md5, pgp) Source 2012-07-10 3KB
  • Downloads (All Versions):
  • 20 downloads in the last day
  • 279 downloads in the last week
  • 779 downloads in the last month