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 7849557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:33:52+00:00 2026-06-02T18:33:52+00:00

Suppose I want to assert that result is in a particular format, I might

  • 0

Suppose I want to assert that result is in a particular format, I might set the return value to something as (True, 'Success')

def batch_move(self, *args, **kwargs):
  ''' 
  Move a batch of files to its respective destinations.
  Return type: tuple (boolean, message)
                      T/F    , 'string' / str(exception)
  '''

  srcs= kwargs.get('srcs', None)
  dests = kwargs.get('dests', None)

  try:
    if srcs and dests:
       # map srcs and dests into dictionary (srcs --> keys, dests --> values)
       src_dest= dict(zip(srcs, dests))     

       for src, dest in src_dest:
         if os.path.exists(src):
           if os.path.exists(dest):
              shutil.rmtree(dest)
           shutil.move(src, dest)   # either way we will proceed to move
         else:
           return (False, '%s does not exist!' % src)
       return (True, 'Success!')

    else:
       return (False, 'Something gone wrong with those kwargs...')
  except Exception as e:
    return (False, e)

In order to get to return (True, 'Success!')

  1. patch os.path.exists with True as return value. But in one unittest I want to skip this, how do I patch that os.path.exists?
    if os.path.exists(dest):  # I want to skip this
         shutil.rmtree(dest)
  1. How do I patch shutil.move(src, dest)? Do I just give True so it doesn’t generate error? What if I want the case it fails and caught an exception? How do I simulate that? (I wouldn’t always know which exception to catch, the primarily reason to use Exception as e).

  2. If I actually pass the function, does it really mean no exception caught and it went through every single line? Or is it because I set `mock_object.return_value = (True, ‘Success!’)?

  3. I am only using two dependencies, do I need to patch out all the external dependencies such as (os, sys, math, datetime) all in one? Or if my function is using other functions (which are refactored)

 def f1(*args, **kwargs):
    f2(..)   # use math, plot, datetime
    f3(..)   # use math and datetime
    f4(..)   # use datetime

    ....

Thanks. Sorry for the long questions. I really want to be good at writing unittests.

  • 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-06-02T18:33:53+00:00Added an answer on June 2, 2026 at 6:33 pm

    I must say that in this particular use-case, I don’t think patching/mocking would be the best alternative…

    For this unit-test, I’d create a structure in the filesystem, run the algorithm on that structure in the disk and check for the different outcomes (all without mocking os nor shutil).

    Usually I tend to use patching mostly when the external setup is difficult or slow (i.e.: maybe having to setup some external database) or when it would halt the test (i.e.: opening up some dialog) or when I want to do something that’s difficult to check otherwise (such as counting if some cache is actually accessed).

    Other than that, patching/mocking too much gives me the impression you have a problem in your design (which wasn’t done with unit-testing in mind), so, breaking it up in smaller chunks to test may help…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to create a set of observers based on type. That is
Suppose I want to put objects that identify a server into a stl set
Suppose I want to implement a curses/console like program in HTML/CSS/Javascript. Examples might be
Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first
Suppose I want to execute code, for example value += 5 inside a namespace
Suppose I want to create a dictionary that maps digits to numbers less than
Suppose I want to define an enum pointing to an array of objects. Can
Suppose I want to open a file in an existing Emacs session using su
Suppose you want to make an async request in JavaScript, but you want to
Suppose I want to count the lines of code in a project. If all

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.