Пример #1
0
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")
Пример #2
0
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"
Пример #3
0
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"
Пример #4
0
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.
Пример #5
0
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")
Пример #6
0
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")
Пример #7
0
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')
Пример #8
0
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'