Last active
November 29, 2024 19:54
-
-
Save zerolagtime/7071e12d0208922742f700af9ca87f9d to your computer and use it in GitHub Desktop.
Demonstrating Python3 Unit Testing with MagicMock, patch, and mock_open (open())
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Test cases for the unique_names module that includes a context manager | |
for the open() call. | |
""" | |
import unittest | |
import unique_names | |
from unittest.mock import Mock, MagicMock, patch, mock_open | |
class TestUnique(unittest.TestCase): | |
def test_get_names_from_file_open(self): | |
# Demonstrate mocking the open() call to read a file. | |
# The contents of the file are mocked in this test, providing | |
# coverage to the whole reader. | |
read_data = 'Jack\nJack\nAmos\n' | |
# m = mock_open(read_data=read_data) | |
with patch('builtins.open', mock_open(read_data=read_data)) as mock_file: | |
names = unique_names.get_names_from_file('filename.txt') | |
self.assertEqual(names, ['Jack', 'Jack', 'Amos']) | |
def test_unique_names_mm(self): | |
# Demonstrate using MagicMock to test the uniqueness filter | |
unique_names.get_names_from_file = MagicMock() | |
unique_names.get_names_from_file.return_value = ['Jack', 'Ross', 'Mike', 'Jack', 'Tom', 'Kate', 'Ross'] | |
names = unique_names.get_unique_names("names.txt") | |
self.assertEqual(names, ['Jack', 'Kate', 'Mike', 'Ross', 'Tom']) | |
@patch('unique_names.get_names_from_file') | |
def test_unique_names_pd(self, mock_get_names_from_file): | |
# Demonstrate using patch to test the uniqueness filter. Patch | |
# is used as a decorator. | |
mock_get_names_from_file.return_value = ['Jack', 'Ross', 'Mike', 'Jack', 'Tom', 'Kate', 'Ross'] | |
names = unique_names.get_unique_names("names.txt") | |
self.assertEqual(names, ['Jack', 'Kate', 'Mike', 'Ross', 'Tom']) | |
def test_unique_names_pcm(self): | |
# Demonstrate using patch as a context manager (with block) | |
with patch('unique_names.get_names_from_file') as mock_get_names_from_file: | |
mock_get_names_from_file.return_value = ['Jack', 'Ross', 'Mike', 'Jack', 'Tom', 'Kate', 'Ross'] | |
names = unique_names.get_unique_names("names.txt") | |
self.assertEqual(names, ['Jack', 'Kate', 'Mike', 'Ross', 'Tom']) | |
if __name__ == "__main__": | |
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_names_from_file(filename): | |
with open(filename, "r") as f: | |
lines = f.readlines() | |
names = [] | |
for line in lines: | |
names.append(line.strip()) | |
return names | |
def get_unique_names(filename): | |
names = get_names_from_file(filename) | |
unique_names = list(set(names)) | |
return sorted(unique_names) | |
if __name__ == "__main__": | |
print(get_unique_names("names.txt")) | |
Author
zerolagtime
commented
Nov 29, 2024
•
# duplicates are: Taylor, Jordan
$ echo -e "Alex\nTaylor\nJordan\nEmily\nCasey\nTaylor\nAaliyah\nJordan\nJamie\nTaylor" > names.txt
$ python3 unique_names.py
['Aaliyah', 'Alex', 'Casey', 'Emily', 'Jamie', 'Jordan', 'Taylor']
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment