Skip to content

gh-109485: Further improve test_future_stmt tests - #109486

Merged
vstinner merged 6 commits into
python:mainfrom
sobolevn:issue-109485
Sep 19, 2023
Merged

gh-109485: Further improve test_future_stmt tests#109486
vstinner merged 6 commits into
python:mainfrom
sobolevn:issue-109485

Conversation

@sobolevn

@sobolevnsobolevn commented Sep 16, 2023

Copy link
Copy Markdown
Member

I've also enabled docstring parametrization, because tests must works the same way with and without docstrings most of the time.

Comment threadLib/test/test_future_stmt/test_future.py Outdated
Comment threadLib/test/test_future_stmt/test_future.py Outdated

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it be possible to check for the error message in assertSyntaxError?

Comment threadLib/test/test_future_stmt/test_future.py
Comment threadLib/test/test_future_stmt/test_future.py
self.assertSyntaxError(code, 3)

def test_badfuture10(self):
def test_bad_future_as_module(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Test name is not clear. The expected error is:

SyntaxError: from __future__ imports must occur at the beginning of the file

@sobolevn

Copy link
Copy Markdown
MemberAuthor

With error messages it should be much better!

Comment threadLib/test/test_future_stmt/test_future.py
lineno,
message=TOP_LEVEL_MSG, offset=1,
parametrize_docstring=True):
code = dedent(code)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems like code first line is always empty. I suggest using:

Suggested change
code=dedent(code)
code=dedent(code.lstrip())

And adapt lineno -= 2 and other references to lineno below.

For example, for me, test_unknown_future_flag() code has only 3 lines and expect an error on the 4th line, it's strange.

@vstinnervstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks! It's way way more readable now! The test methods have better name, it's easier to understand the purpose of the test, and you added many checks!

code = dedent(code.lstrip('\n'))
for trim_docstring in ([False, True] if parametrize_docstring else [False]):
with self.subTest(code=code, trim_docstring=trim_docstring):
if trim_docstring:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Last question: instead of copy/paste '''Docstring''' in each test. Would it make sense to add it here instead? I'm waiting for your opinion to decide if we merge the change like that, or you prefer to remove docstrings in tests and add it here instead.

@brettcannon
brettcannon removed their request for review September 18, 2023 18:07
@vstinner
vstinner merged commit 94c95d4 into python:mainSep 19, 2023
@vstinner

Copy link
Copy Markdown
Member

Thanks a lot @sobolevn, it's way better! I edited the final message.

csm10495 pushed a commit to csm10495/cpython that referenced this pull request Sep 28, 2023
)
Add assertSyntaxError() which run tests with an additional docstring
and without docstring, and checks for the error message.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@sobolevn@vstinner