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

  • SEARCH
  • Home
  • 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 6542729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:15:44+00:00 2026-05-25T11:15:44+00:00

I have some special cases that I need to test for in django. I

  • 0

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!

  • 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-05-25T11:15:44+00:00Added an answer on May 25, 2026 at 11:15 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some special parameters to all my wcf service methods that are handled
I have a server application that receives some special TCP packet from a client
I have a php script validation that looks for some special chars and deletes
In some special cases you will need a list of textboxes (to deal with
I have a TableLayout inside a ScrollView, because of some special needs, I need
I have an issue while storing some special character values into db. For e.g.
I use special characters (swedish letters åäö). Now, I have some folders, which contains
There are some products for which I would like to have a special checkout
I have some data loaded as a np.ndarray and need to convert it to
I have some arbitrary pixel data that I want to save as a PNG.

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.