def test_returnCorrectString(monkeypatch): mock_file = MagicMock() mock_file.readline = MagicMock(return_value="test line") mock_open = MagicMock(return_value=mock_file) monkeypatch.setattr("builtins.open", mock_open) result = readFromFile("blah") mock_open.assert_called_once_with("blah", "r") assert result == "test line"
def test_throwsExceptionWithBadFile(monkeypatch): mock_file = MagicMock() mock_file.readline = MagicMock(return_value='test line') mock_open = MagicMock(return_value=mock_file) monkeypatch.setattr('builtins.open', mock_open) mock_exists = MagicMock(return_value=False) monkeypatch.setattr('os.path.exists', mock_exists) with raises(Exception): result = readFromFile('blah')
def test_throwExceptionWithBadFile(mock_open, monkeypatch): #mock_file = MagicMock() #mock_file.readline = MagicMock(return_value="test line") #mock_open = MagicMock(return_value=mock_file) #monkeypatch.setattr("builtins.open", mock_open) # Refactor mock_exists = MagicMock(return_value=False) monkeypatch.setattr("os.path.exists", mock_exists) with raises(Exception): result = readFromFile("blah")
def test_returnCorrectString(monkeypatch): mock_file = MagicMock() mock_file.readline = MagicMock(return_value='test line') mock_open = MagicMock(return_value=mock_file) monkeypatch.setattr('builtins.open', mock_open) mock_exists = MagicMock(return_value=True) monkeypatch.setattr('os.path.exists', mock_exists) result = readFromFile('blah') mock_open.assert_called_once_with('blah', 'r') assert result == 'test line'
def test_returnsCorrectString(mock_open, monkeypatch): #Refactor #mock_file = MagicMock() #mock_file.readline = MagicMock(return_value="test line") #mock_open = MagicMock(return_value=mock_file) #monkeypatch.setattr("builtins.open", mock_open) mock_exists = MagicMock(return_value=True) monkeypatch.setattr("os.path.exists", mock_exists) result = readFromFile("blah") mock_open.assert_called_once_with("blah", "r") assert result == "test line"
def test_throwsExceptionWithBadFile(mock_open, monkeypatch): mock_exists = MagicMock(return_value=False) monkeypatch.setattr("os.path.exists", mock_exists) with raises(Exception): result = readFromFile("blah")
def test_returnsCorrectString(mock_open, monkeypatch): mock_exists = MagicMock(return_value=True) monkeypatch.setattr("os.path.exists", mock_exists) result = readFromFile("blah") mock_open.assert_called_once_with("blah", "r") assert result == "test line"
def test_throwsExceptionWhenNoFile(mock_open, monkeypatch): mock_exists = MagicMock(return_value=False) monkeypatch.setattr("os.path.exists", mock_exists) with pytest.raises(Exception): result = readFromFile("fake file")