0

I'm looking to unit test a class method that makes a function call to popen and performs some operations on it.

from os import popen

class myclass(object):
    ''' Generic class. '''

    def do_something(self):
        ret = popen("cat some_file")
        # do some work with the returned value here
        return modified_ret

I need to control what is returned from that popen call in order to test the method. What's the best way of going about that?

edit: I also have multiple methods that use popen in this single class; so if I could override it on the instance rather than the whole class that would be great.

noctorum
  • 3
  • 1

1 Answers1

1

If I am interpreting the question correctly, it seems that you are looking for a mocking library for testing.

Take a look at this

What is your favorite Python mocking library?

A mocking library allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

Community
  • 1
  • 1
DanGar
  • 3,018
  • 17
  • 17