Skip to content
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

TST: pytest.raises vs. tm.assert_raises_regex #16521

Closed
gfyoung opened this issue May 28, 2017 · 12 comments
Closed

TST: pytest.raises vs. tm.assert_raises_regex #16521

gfyoung opened this issue May 28, 2017 · 12 comments
Labels
Error Reporting Incorrect or improved errors from pandas Testing pandas testing functions or related to the test suite
Milestone

Comments

@gfyoung
Copy link
Member

gfyoung commented May 28, 2017

I'm wondering whether we should explicitly say in the docs that we prefer tm.assert_raises_regex over pytest.raises unless there is no error message to provide. The former is a lot more informative from a dev-perspective as to why certain code should fail.

Thoughts?

(If we can agree that we do prefer the former, I also move to convert some of the tests to use tm.assert_raises_regex instead of pytest.raises).

@jreback
Copy link
Contributor

jreback commented May 30, 2017

I don't think this is necessary to always check the actual content of the error, unless the content of the message is relevant (maybe to distinguish cases).

@jreback jreback added Error Reporting Incorrect or improved errors from pandas Testing pandas testing functions or related to the test suite labels May 30, 2017
@jreback jreback added this to the Next Major Release milestone May 30, 2017
@gfyoung
Copy link
Member Author

gfyoung commented May 30, 2017

@jreback : Fair enough, but I think in many cases, we can though. One of my arguments for using tm.assert_raises_regex is that you can get a better idea of WHY code should fail beyond just the exception being raised.

So sure, I don't think all of them need to be converted, but I still do think tm.assert_raises_regex should be preferred by default. How does that sound?

@TomAugspurger
Copy link
Contributor

FWIW, I would like to avoid calling pytest-specific methods where possible, to make a migration away from pytest easier in the far future, if that's ever necessary.

We could add a tm.assert_raises context manager that simply wraps pytest.raises at the moment.

@gfyoung
Copy link
Member Author

gfyoung commented May 30, 2017

@TomAugspurger : That is possible, though tm.assert_raises_regex(exc, None) also does the trick.

@jreback
Copy link
Contributor

jreback commented May 30, 2017

FWIW, I would like to avoid calling pytest-specific methods where possible, to make a migration away from pytest easier in the far future, if that's ever necessary.

I don't think this is a good reason. The good reason is that simply changing an error message can cause failures which are not immediately obvious (of course you want things to fail, but this mean that your regext could be too strict).

@gfyoung
Copy link
Member Author

gfyoung commented May 30, 2017

@jreback : I think @TomAugspurger was somewhat separate from the main discussion. That being said, what you said is one reason why I think we should prefer tm.assert_raises_regex by default.

gfyoung added a commit to forking-repos/pandas that referenced this issue Jul 24, 2017
gfyoung added a commit to forking-repos/pandas that referenced this issue Jul 24, 2017
@gfyoung
Copy link
Member Author

gfyoung commented Jul 24, 2017

I'm going to close this, since I think there is sufficient discussion on this matter (at least for myself 😄). Can always revisit if need be.

@gfyoung gfyoung closed this as completed Jul 24, 2017
@gfyoung gfyoung modified the milestones: No action, Next Major Release Jul 24, 2017
@gfyoung gfyoung removed this from the No action milestone Nov 7, 2018
@gfyoung gfyoung reopened this Nov 7, 2018
@gfyoung
Copy link
Member Author

gfyoung commented Nov 7, 2018

@pandas-dev/pandas-core

I'd like to re-open this in part because pytest.raises now supports regex matching (via the match parameter), which is exactly what tm.assert_raises_regex is designed to do.

IIUC, there is some effort to make things for pytest-idiomatic. Thus, the question is: do we want to use pytest.raises across the board and drop tm.assert_raises_regex ?

I would bring up a similar question for pytest.warns, but I understand that it currently does not check stacklevel unlike our implementation --> I'll focus just on tm.assert_raises_regex.

@WillAyd
Copy link
Member

WillAyd commented Nov 7, 2018

I'd be +1 on this. Doesn't seem like a long term fit to maintain this method in our code base

@jorisvandenbossche
Copy link
Member

Also +1 for using pytest.raises now it has all functionality (I think?) we need.

For pytest.warns, maybe it would a good idea if somebody finds the time to ask pytest if they would be interested in adding such a feature.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Nov 8, 2018 via email

gfyoung added a commit to forking-repos/pandas that referenced this issue Nov 9, 2018
pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.
gfyoung added a commit to forking-repos/pandas that referenced this issue Nov 9, 2018
pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.
@gfyoung
Copy link
Member Author

gfyoung commented Nov 9, 2018

The deed has been done! See #23592.

gfyoung added a commit to forking-repos/pandas that referenced this issue Nov 9, 2018
pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.
@jreback jreback added this to the 0.24.0 milestone Nov 9, 2018
gfyoung added a commit to forking-repos/pandas that referenced this issue Nov 10, 2018
pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.
gfyoung added a commit to forking-repos/pandas that referenced this issue Nov 10, 2018
pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.
jreback pushed a commit that referenced this issue Nov 10, 2018
* MAINT: tm.assert_raises_regex --> pytest.raises

pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes gh-16521.

* Don't remove, just deprecate assert_raises_regex

* CLN: Test cleanups and follow-ups
JustinZhengBC pushed a commit to JustinZhengBC/pandas that referenced this issue Nov 14, 2018
* MAINT: tm.assert_raises_regex --> pytest.raises

pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.

* Don't remove, just deprecate assert_raises_regex

* CLN: Test cleanups and follow-ups
tm9k1 pushed a commit to tm9k1/pandas that referenced this issue Nov 19, 2018
* MAINT: tm.assert_raises_regex --> pytest.raises

pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.

* Don't remove, just deprecate assert_raises_regex

* CLN: Test cleanups and follow-ups
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this issue Feb 28, 2019
* MAINT: tm.assert_raises_regex --> pytest.raises

pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.

* Don't remove, just deprecate assert_raises_regex

* CLN: Test cleanups and follow-ups
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this issue Feb 28, 2019
* MAINT: tm.assert_raises_regex --> pytest.raises

pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.

* Don't remove, just deprecate assert_raises_regex

* CLN: Test cleanups and follow-ups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Testing pandas testing functions or related to the test suite
Projects
None yet
Development

No branches or pull requests

5 participants