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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:51:01+00:00 2026-06-04T19:51:01+00:00

I have a python method does the following: list the files under a directory

  • 0

I have a python method does the following:

  1. list the files under a directory using os.listdir(/test)
  2. regex match some of the files under the directory, put the files in a list
  3. read the contents out of files in the list, do some aggregation stuff.

Obviously, the only interesting part for me to test in my case is 2, 3, so 1 is definitely something I want to mock against. I started doing patch file creation/deletion under /test folder in my setUp() and tearDown(). But colleague told me it’s not good idea to do I/O in unitest.

so what’s the best way to mock build in os.listdir() in my unitest? or what’s the alternatives?

Is there anything I can do to achieve something like:

setUp() {
    #mock a few files eg.test1.txt, test2.txt, test3.txt under directory /test 
    #without physically creating them using I/O
}
tearDown() {
   #whatever cleanup required 
}
  • 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-04T19:51:03+00:00Added an answer on June 4, 2026 at 7:51 pm

    What about using the Mock module?

    >>> import os
    >>> from mock import MagicMock
    >>> os.listdir = MagicMock(return_value=['file1.txt', 'file2.txt', 'file3.txt'])
    >>> os.listdir('./test')
    ['file1.txt', 'file2.txt', 'file3.txt']
    

    If you don’t want to mokey-patch (ie. break) os, then you could use mock_os or the likes.

    Read about starting and stopping:
    http://docs.python.org/dev/py3k/library/unittest.mock.html#patch-methods-start-and-stop

    And:
    http://docs.python.org/dev/py3k/library/unittest.mock.html#quick-guide

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

Sidebar

Related Questions

In Python compiled regex patterns have a findall method that does the following: Return
I'm new to Python. I have a method that begins: def foo(self, list): length
Does Python have extension methods like C#? Is it possible to call a method
Why doesn't Python allow modules to have a __call__ method? (Beyond the obvious that
I have the following simple methods for writing a python object to a file
I have a Python 3 class method for rescaling values that looks like this:
The following Python snippet does exactly what I mean: def function(a, b, c): print(%i
I have some python module, which has a class ModuleClass and I can't modify
I have the following questions about tweepy python module 1.I am trying to retrieve
I have the following XPath to match authors name in an Amazon page: //div[@class='pTitle']/span[@class='small

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.