Skip to content

Commit

Permalink
added a test case to test that file not found error is thrown when tr…
Browse files Browse the repository at this point in the history
…ying to open file that doesnt exist
  • Loading branch information
yusufjimoh committed Jun 3, 2024
1 parent 39f0a9e commit 19e0593
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions exercises-python/template/ex01_basics/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python3

def read_ascii_file(param):
def read_ascii_file(filename):
try:
f = open(param, 'r')
f = open(filename, 'r')
return f.read()
except FileNotFoundError:
print("Oops! That was not a valid file. Try again...")
except FileNotFoundError as e:
raise FileNotFoundError(f"The file {filename} was not found.")

def main():
read_ascii_file("file.txt")
Expand Down
8 changes: 8 additions & 0 deletions exercises-python/template/ex01_basics/test_wordcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ def test_read_file(self, mock_file):
self.assertEqual(content, expected_content)
mock_file.assert_called_once_with(self.filename, 'r')

@patch('builtins.open', side_effect=FileNotFoundError)
def test_read_file_raise_file_found_error(self, mock_file):
with self.assertRaises(FileNotFoundError):
read_ascii_file(self.filename)
mock_file.assert_called_once_with(self.filename, 'r')





0 comments on commit 19e0593

Please sign in to comment.