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

bpo-41322: add two unit tests for deprecation of test return values #27846

Merged

Conversation

akulakov
Copy link
Contributor

@akulakov akulakov commented Aug 20, 2021

@serhiy-storchaka
Copy link
Member

While we are here, please document this change in the unittest module documentation and add an entry in the What's New document.

@serhiy-storchaka
Copy link
Member

I would also add stacklevel=3 in calls of warnings.warn() and change tests to test it:

with self.assertWarns(DeprecationWarning) as w:
    Foo('test_func').run()
self.assertIn('It is deprecated to return a value!=None', w.warnings[0].message)
self.assertIn('test_func', w.warnings[0].message)
self.assertEqual(w.warnings[0].filename, __file__)

@akulakov
Copy link
Contributor Author

akulakov commented Aug 20, 2021

While we are here, please document this change in the unittest module documentation and add an entry in the What's New document.

My reasoning for not adding the change to the docs was that the pattern of use of test methods and the way they are described in the docs don't leave any reason for returning a value from a test method, so it seems like the only case when that would happen would be a mistake (possibly to be used by 3rd party code, but that seems unlikely).

But I don't see any harm in adding it to docs, see below. I'm curious if there are scenarios where users might want to return values from test method and this change to the docs would dissuade them.

I'm not sure if the docs should try to explain the reason for the change, it seems like a short explanation would be puzzling and a good explanation would be too long.

Proposed text:

Changed in 3.11:
  The behavior of returning a value from a test (other than the default None value), is now deprecated.

@akulakov
Copy link
Contributor Author

I will look more into it, just posting here in case you know why it doesn't work in the same way...

I got it now, please ignore..

Comment on lines 180 to 182
self.assertIn('It is deprecated to return a value!=None', w.warnings[0].message.args[0])
self.assertIn('test', w.warnings[0].message.args[0])
self.assertIn(w.warnings[0].filename, __file__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to move this outside of the with block.

Suggested change
self.assertIn('It is deprecated to return a value!=None', w.warnings[0].message.args[0])
self.assertIn('test', w.warnings[0].message.args[0])
self.assertIn(w.warnings[0].filename, __file__)
self.assertIn('It is deprecated to return a value!=None', w.warnings[0].message.args[0])
self.assertIn('test', w.warnings[0].message.args[0])
self.assertIn(w.warnings[0].filename, __file__)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not assertIn, but assertEqual.

with self.assertWarns(DeprecationWarning) as w:
Test('test').run()
self.assertIn('It is deprecated to return a value!=None', w.warnings[0].message.args[0])
self.assertIn('test', w.warnings[0].message.args[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'test' is too general name. This substring is a part of the warning message ("... from a test case ..."), so the test is passed in any case. Use more specific names, like test1, test2, test_func, test_gen.

Also, would not be better to test str(w.warnings[0].message) instead of w.warnings[0].message.args[0]?

Test('test1').run()
self.assertIn('It is deprecated to return a value!=None', str(w.warnings[0].message))
self.assertIn('test1', str(w.warnings[0].message))
self.assertEquals(w.warnings[0].filename, __file__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertEquals is a deprecated alias. Use assertEqual.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, somehow missed the deprecation warnings in a PR for deprecation warnings :) thanks for catching that.

@akulakov
Copy link
Contributor Author

@serhiy-storchaka thanks for reviews! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants