-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
STY: use pytest.raises context syntax #24676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
""" | ||
import mmap | ||
import os | ||
import re | ||
|
||
import pytest | ||
|
||
|
@@ -146,7 +145,16 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext): | |
pytest.importorskip(module) | ||
|
||
path = os.path.join(HERE, 'data', 'does_not_exist.' + fn_ext) | ||
with pytest.raises(error_class): | ||
msg1 = (r"File (b')?.+does_not_exist\.{}'? does not exist" | ||
.format(fn_ext)) | ||
msg2 = (r"\[Errno 2\] No such file or directory: '.+does_not_exist" | ||
r"\.{}'").format(fn_ext) | ||
msg3 = "Expected object or value" | ||
msg4 = "path_or_buf needs to be a string file path or file-like" | ||
msg5 = (r"\[Errno 2\] File .+does_not_exist\.{} does not exist:" | ||
r" '.+does_not_exist\.{}'").format(fn_ext, fn_ext) | ||
with pytest.raises(error_class, match=r"({}|{}|{}|{}|{})".format( | ||
msg1, msg2, msg3, msg4, msg5)): | ||
reader(path) | ||
|
||
@pytest.mark.parametrize('reader, module, error_class, fn_ext', [ | ||
|
@@ -169,14 +177,26 @@ def test_read_expands_user_home_dir(self, reader, module, | |
monkeypatch.setattr(icom, '_expand_user', | ||
lambda x: os.path.join('foo', x)) | ||
|
||
message = "".join(["foo", os.path.sep, "does_not_exist.", fn_ext]) | ||
|
||
with pytest.raises(error_class, message=re.escape(message)): | ||
msg1 = (r"File (b')?.+does_not_exist\.{}'? does not exist" | ||
.format(fn_ext)) | ||
msg2 = (r"\[Errno 2\] No such file or directory:" | ||
r" '.+does_not_exist\.{}'").format(fn_ext) | ||
msg3 = "Unexpected character found when decoding 'false'" | ||
msg4 = "path_or_buf needs to be a string file path or file-like" | ||
msg5 = (r"\[Errno 2\] File .+does_not_exist\.{} does not exist:" | ||
r" '.+does_not_exist\.{}'").format(fn_ext, fn_ext) | ||
|
||
with pytest.raises(error_class, match=r"({}|{}|{}|{}|{})".format( | ||
msg1, msg2, msg3, msg4, msg5)): | ||
reader(path) | ||
|
||
def test_read_non_existant_read_table(self): | ||
path = os.path.join(HERE, 'data', 'does_not_exist.' + 'csv') | ||
with pytest.raises(FileNotFoundError): | ||
msg1 = r"File b'.+does_not_exist\.csv' does not exist" | ||
msg2 = (r"\[Errno 2\] File .+does_not_exist\.csv does not exist:" | ||
r" '.+does_not_exist\.csv'") | ||
with pytest.raises(FileNotFoundError, match=r"({}|{})".format( | ||
msg1, msg2)): | ||
with tm.assert_produces_warning(FutureWarning): | ||
pd.read_table(path) | ||
|
||
|
@@ -326,7 +346,8 @@ def test_next(self, mmap_file): | |
next_line = next(wrapper) | ||
assert next_line.strip() == line.strip() | ||
|
||
pytest.raises(StopIteration, next, wrapper) | ||
with pytest.raises(StopIteration, match=r'^$'): | ||
next(wrapper) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @h-vetinari well spotted. thanks. i'll get that sorted. |
||
|
||
def test_unknown_engine(self): | ||
with tm.ensure_clean() as path: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see at least two distinct tests in this one. In the interest of smaller, more modular, testing, let's at least break it up here where I've commented.