def test_throws_exception_with_bad_file(monkeypatch): mock_exists = MagicMock( return_value=False ) # Mock the "os.path.exists" functionality so that I can control when it returns True or False depending on the test case. monkeypatch.setattr("os.path.exists", mock_exists) with raises(Exception): read_from_file("blah")
def test_returnsCorrectString(mock_open, monkeypatch): # Update the test case to mock out the os.path.exist, # and have it return true for that particular test case: mock_exists = MagicMock(return_value = True) monkeypatch.setattr("os.path.exists", mock_exists) result = read_from_file("mock_file.txt") mock_open.assert_called_once_with("mock_file.txt", "r") assert result == "Mock Title"
def test_returns_correct_string(mock_open, monkeypatch): mock_exists = MagicMock(return_value=True) monkeypatch.setattr("os.path.exists", mock_exists) result = read_from_file("blah") mock_open.assert_called_once_with("blah", "r") assert result == "test line"
def test_returns_correct_string(mock_open, monkeypatch): mock_exists = MagicMock( return_value=True ) # Mock the "os.path.exists" functionality so that I can control when it returns True or False depending on the test case. monkeypatch.setattr("os.path.exists", mock_exists) result = read_from_file("blah") # Call the read_from_file function. mock_open.assert_called_once_with( "blah", "r" ) # validate that the open function was called with the correct parameters. assert result == "test line" # validate that the expected test string was returned.
def test_throwsExceptionWithBadFile(mock_open, monkeypatch): mock_exists = MagicMock(return_value = False) monkeypatch.setattr("os.path.exists", mock_exists) with raises(Exception): result = read_from_file("mock_file.txt")
def test_throws_exception_if_file_does_not_exist(mock_open, monkeypatch): mock_exists = MagicMock(return_value=False) monkeypatch.setattr("os.path.exists", mock_exists) with pytest.raises(Exception): result = read_from_file("blah")
def test_throws_exceptions_with_bad_file(mock_open, monkeypatch): mock_exists = MagicMock(return_value=False) monkeypatch.setattr('os.path.exists', mock_exists) with raises(Exception): result = read_from_file('blah')
def test_returns_correct_string(mock_open, monkeypatch): mock_exists = MagicMock(return_value=True) monkeypatch.setattr('os.path.exists', mock_exists) result = read_from_file('blah') mock_open.assert_called_once_with('blah', 'r') assert result == 'test line'