Search code examples
pythonunit-testingffmpegffprobe

How to mock an .mpg file for unit tests?


Ok. I have a class which runs this command:

ffmpeg_command = "ffprobe -v quiet -print_format json -show_format -show_streams %s" % self.absolute_path

Where self.absolute_path is a path to a movie, let's say .mpg file.

The file I am using for testing is 4GB large and I don't want to commit it inside my GIT repo.

So I was thinking of mocking this file and creating a file called:

mock.mpg

Which would return the same string as the actual mpg movie when supplied as input to ffprobe command. Is that possible?

Or what other approach should I choose?

This is my class:

  class Movie(object):

      absolute_path = None
      info = None

      def __init__(self, path):
          self.absolute_path = "%s/%s" % (os.getcwd(), path)
          if(os.path.exists(self.absolute_path) is False):
              raise IOError("File does not exist")

          self.info = json.loads(self.get_info())

    def get_info(self):
          ffmpeg_command = "ffprobe -v quiet -print_format json -show_format -show_streams %s" % self.absolute_path
          return subprocess.check_output(ffmpeg_command, shell=True)

This is how I will be unit testing it:

  class MovieTest(unittest.TestCase):

      def test_foo(self):
          movie = Movie("tests/test_1.mpg") # this file should be a mock file!!!

Solution

  • I would suggest that you wrap the ffmpeg dependency.

    Supply a mock for this instead. Wrapping external tools/libraries is a good idea anyway. If any interfaces changes, you need only to fix the wrapper anf not every connection from your code and into ffmpeg. The wrapper only need to be paper thin to get the benifit from easy unittesting.