2

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!!!
Richard Knop
  • 81,041
  • 149
  • 392
  • 552

2 Answers2

4

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.

daramarak
  • 6,115
  • 1
  • 31
  • 50
2

Trust ffmpeg to do it's job correctly. Then all you have to do in your test is verify that the command is correct for the given input.

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • But I want to be able to also test my logic which will depend on the ffprobe output from the mpg file (to test that my method get_duration returns correct float etc) so I need to mock it somehow. I just can't figure how to do this without having an actual mpg file inside tests directory. – Richard Knop Jun 28 '12 at 11:07
  • @RichardKnop Simply Mock the ffpeg dependency, create a wrapper class if necessary. Then in your tests validate the expected behaviour around the input and output of this. – Rich O'Kelly Jun 28 '12 at 11:22
  • @RichO'Kelly yeah - you need to be able to supply a mock version of ffmpeg, that way you don't actually need to have an avi file in place to test your code. @RichardKnop I'm using the `mock` library for node. It seems to work quite well. – robertmain Oct 20 '17 at 14:15