0
  • I have a function that wraps os remove.
  • I have unit test setup as follows:

    self.assertRaises(FileNotFoundError, my_rm(bad_file_path))

bad_file_path does not exist, so it throws exception. However, the above still fails testing. How could I test for FileNotFoundError, if possible?

Jacob
  • 349
  • 1
  • 2
  • 12

1 Answers1

1

The documentation recommends that you use with to run assertRaises tests, like this:

with self.assertRaises(FileNotFoundError):
    my_rm(bad_file_path)

Otherwise, you would have to pass in the function and arguments separately, like this:

self.assertRaises(FileNotFoundError, my_rm, bad_file_path)

What you're currently doing is calling my_rm(bad_file_path) and trying to pass the result of that call to self.assertRaises(). Since an exception occurs, the test crashes.

Aurora0001
  • 13,139
  • 5
  • 50
  • 53