Praveen Gollakota
Praveen Gollakota

Reputation: 38960

How to augment a class from an external library with own methods?

I have some special cases that I need to test for in django. I am trying to extend the the existing django tests by writing my own test cases. Here is how I am currently doing it.

from django.tests import TestCase

# define my own method as a function
def assertOptionsEqual(self, first, second):
    # logic here
    pass

# Attach the method to the TestCase class. This feels inelegant!
TestCase.assertOptionsEqual = assertOptionsEqual

# tests go here
class KnownGoodInputs(TestCase):
    def test_good_options(self):
        self.assertOptionsEqual(...)

While this works, defining a method as a function with self as the first parameter and then attaching it to TestCase feels inelegant. Is there a better way of augmenting the TestCase class with my own methods? I can do this ...

class MyTestCase(TestCase):
    def assertOptionsEqual(self, first, second):
        ...

and use MyTestCase for all tests, but was wondering if there was a better alternative. Thanks!

Upvotes: 2

Views: 1043

Answers (1)

stderr
stderr

Reputation: 8732

I think you've covered both options. You can either subclass or monkeypatch. Typically, monkeypatching, actually changing the 3rd party class at runtime is frowned upon but depending on what change you need to make it may be the only way to work around a bug or make sure that every time that class is used it has your new method.

Since the only tests that use your method will be your tests monkeypatching is unnecessary and it's quite reasonable to subclass TestCase. Typically you'd use monkeypatching when you needed to augment a method of a existing class. For example, if you wanted calls to TestCase.assertEqual in your existing test cases to be augmented with logic to compare to Option objects you could monkeypatch TestCase.assertEqual to include your custom logic plus its normal logic by doing something like:

originalAssertEqual = TestCase.assertEqual
def newAssertEqual(self, first, second):
    result = originalAssertEqual(first, second)
    if isinstance(first, Option) and isinstance(second, Option):
        # do your custom comparison
    return result
TestCase.assertEqual = newAssertEqual 

However, it seems that at least in this example that both subclasses and monkeypatches are unnecessary.

Assuming that the issue is that calling self.assertEqual(firstOptions, secondOptions) fails even though the Option instances are equal you don't need to write a new assertOptionsEqual method. You probably just need your Option objects to define __eq__ properly.

So assuming that you've got:

class KnownGoodInputs(TestCase):
    def test_good_options(self):
        first, second = systemUnderTestGetOptions(...)
        self.assertOptionsEqual(first, second)

What are the classes of first and second above?

For all Python builtin types assertEqual should work. For a custom Option class just do something like this:

class Option(object): def init(self): use_foo = False use_bar = True

def __eq__(self, other):
    if (self.use_foo == other.use_foo and
        self.use_bar == other.use_bar):
        return True
    return False

Then assuming that first and second are instances of Option you can write your test just as:

class KnownGoodInputs(TestCase):
    def test_good_options(self):
        first, second = systemUnderTestGetOptions(...)
        self.assertEqual(first, second)

Upvotes: 1

Related Questions