def METHOD_NAME(self, fn, filename, expected_exception,
                   check_fn_in_exception = True):
    with self.assertRaises(expected_exception) as c:
        fn(filename)
    exc_filename = c.exception.filename
    # the "filename" exception attribute may be encoded
    if isinstance(exc_filename, str):
        filename = filename.encode(sys.getfilesystemencoding())
    if check_fn_in_exception:
        self.assertEqual(exc_filename, filename, "Function '%s(%r) failed "
                         "with bad filename in the exception: %r" %
                         (fn.__name__, filename, exc_filename))