Python Forum
unittest for nested function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unittest for nested function
#1
Hello all,

Have to write couple unittests for nested function.
This is simplified version

def processSubscription(
        self,
        command,
        **conditions
):
    def _matchConditions(request_conditions, command_conditions):
        if request_conditions in command_conditions:
            return True
        else
            return False

    if _matchConditions(conditions, command_conditions):
        subscribers = subscription['emails']
        send_mail(subscribers)

I want to capture the return value from _matchConditions
for my tests and also mock send_mail function,
so mails are not actually send on match.
Reply
#2
Create a mock send_mail function. That will prevent sending the actual mails, and you can use whether it was triggered or not to test whether _matchConditions was True or False.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
You might try to isolate the behavior of the parts you want to test. Since you are using direct references to the parts of the the function you would like to test, this is a problem.

One possible way to reorganize your functions:

def processSubscription(
        self,
        mail_send_function,
        command,
        **conditions
):
    
    def _matchConditions(request_conditions, command_conditions):
        if request_conditions in command_conditions:
            return True
        else
            return False
 
    if _matchConditions(conditions, command_conditions):
        subscribers = subscription['emails']
        mail_send_function(subscribers)
In this case, we're just moving the definition of the activity for sending an email from the processSubscription definition. You can supply the behavior at runtime, or if you are running a test, provide a function that does nothing.

You can also do the same process for _matchConditions.

The advantage is that while you are testing processSubscription you can supply the behavior to test what ever scenarios you have in mind.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 320 Feb-25-2024, 12:51 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 619 Oct-02-2023, 09:21 AM
Last Post: deanhystad
Question Unwanted execution of unittest ThomasFab 9 2,069 Nov-15-2022, 05:33 PM
Last Post: snippsat
  unittest.mock for an api key silver 3 1,396 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  return vs. print in nested function example Mark17 4 1,756 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Ran 0 tests in 0.000s - unittest Peaches 8 5,123 Dec-31-2021, 08:58 AM
Last Post: Peaches
  Exit function from nested function based on user input Turtle 5 2,926 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,687 May-02-2021, 12:21 PM
Last Post: Gribouillis
Sad Problem with Unittest mhanusek 1 3,768 Nov-12-2020, 04:58 PM
Last Post: Gribouillis
  Nested function problem chipx 8 3,498 Oct-21-2020, 11:56 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020