dragoon
dragoon

Reputation: 5743

python/django unittest function override

I have a time-consuming method with non-predefined number of iterations inside it that I need to test:

def testmethod():
    objects = get_objects()
    for objects in objects:
        # Time-consuming iterations
        do_something(object)

One iteration is sufficient to test for me. What is the best practice to test this method with one iteration only?

Upvotes: 0

Views: 608

Answers (2)

Sam Dolan
Sam Dolan

Reputation: 32532

Update:

I misread the original question, so here's how I would solve the problem without altering the source code:

Lambda out your call to get objects to return a single object. For example:

from your_module import get_objects

def test_testmethdod(self):
    original_get_objects = get_objects
    one_object = YourObject()
    get_objects = lambda : [one_object,]

    # ...
    # your tests here
    # ...

    # Reset the original function, so it doesn't mess up other tests
    get_objects = original_get_objects

Upvotes: 2

Alex Gaynor
Alex Gaynor

Reputation: 15019

Perhaps turn your method into

def my_method(self, objs=None):
    if objs is None:
        objs = get_objects()
    for obj in objs:
        do_something(obj)

Then in your test you can call it with a custom objs parameter.

Upvotes: 2

Related Questions