Let's say that you are using a class that comes from a vendor package that you can't control. A class that helps you manage your video files:


    class VideoManager(object):
    """
    The class that we must mock
    """
    def __init__(self, file_name):
      self.file = open(file_name, 'r')

    @property
    def is_video(self):
      return (self.file.content_type in VIDEO_CONTENT_TYPES and
          self.file.content_type not in UNSUPPORTED_CONTENT_TYPES)

    @property
    def length(self):
      """
      length in milliseconds
      """
      return self.file.milliseconds * 1000

Now you want to use that package to create a web form that allows your users to upload and validate their videos:


  # file: forms.py
  # import your external package
  from video_tools import VideoManager

  class UploadVideoForm(object):
    def __init__(self, file_name, file_data):
        self.file_manager = VideoManager(file_name)

    def validate(self):
      if not self.file_manager.is_video:
        raise InvalidVideoFile()
      if self.file_manager.length <= MIN_VIDEO_LENGTH:
        raise VideoTooShort()
      if self.file_manager.length > MAX_VIDEO_LENGTH:
        raise VideoTooLong()

when writing your unit tests you could have a sample of videos and upload them to test them. Or you could mock the VideoManager class to easily test your form.


  # file: forms.py
  def test_invalid_form(self):
    with patch("forms.VideoManager") as video_manager_mock:
      type(video_manager_mock.return_value).is_video = PropertyMock(return_value=False)
      my_form = UploadVideoForm()
      self.assertRaises(InvalidVideoFile, my_form.validate)

    with patch("forms.VideoManager") as video_manager_mock:
      type(video_manager_mock.return_value).length = PropertyMock(return_value=MIN_VIDEO_LENGTH - 1)
      my_form = UploadVideoForm()
      self.assertRaises(VideoTooShort, my_form.validate)

    with patch("forms.VideoManager") as video_manager_mock:
      type(video_manager_mock.return_value).length = PropertyMock(return_value=MAX_VIDEO_LENGTH + 1)
      my_form = UploadVideoForm()
      self.assertRaises(VideoTooLong, my_form.validate)

Here I'm patching the VideoManager properties so we could test the logic of the form validate method. This is the only way I found to mock a property.

Hope this helps