Skip to content

gh-131290: ensure that test files can be executed as standalone scripts - #131371

Merged
picnixz merged 22 commits into
python:mainfrom
MaximGit1:fix-for-running-tests-issue-131290
Apr 12, 2025
Merged

gh-131290: ensure that test files can be executed as standalone scripts#131371
picnixz merged 22 commits into
python:mainfrom
MaximGit1:fix-for-running-tests-issue-131290

Conversation

@MaximGit1

@MaximGit1MaximGit1 commented Mar 17, 2025

Copy link
Copy Markdown
Contributor

Now tests are launched via ./python.exe and work correctly

added environment variable value "PYTHONREGRTEST_UNICODE_GUARD0" when calling the test directly
added spec for test when calling directly
@ghost

ghost commented Mar 17, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-appbedevere-appBot added the tests Tests in the Lib/test dir label Mar 17, 2025
@bedevere-app

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixzpicnixz changed the title gh-131290: Several tests are modified when called directly via ./python Lib/test/...gh-131290: fix direct test files invocationMar 17, 2025
@sobolevn
sobolevn requested a review from Eclips4March 17, 2025 17:30
Comment threadMisc/NEWS.d/next/Tests/2025-03-17-19-47-27.gh-issue-131290.NyCIXR.rst Outdated
Comment threadLib/test/test_metaclass.py
Comment threadLib/test/test_pyclbr.py Outdated
if __name__ == "__main__":
import importlib.util

# Adding the __spec__ attribute to the __main__ module

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.

Which test is failing because of the lack of __spec__?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

test_others

user\cpython\Lib\test\test_pyclbr.py", line 226, in test_others
cm(
~~^
'pdb',
^^^^^^
# pyclbr does not handle elegantly `typing` or properties
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ignore=('Union', '_ModuleTarget', '_ScriptTarget', '_ZipTarget', 'curframe_locals'),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "user\cpython\Lib\test\test_pyclbr.py", line 142, in checkModule
self.assertHaskey(dict, name, ignore)
~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "user\cpython\Lib\test\test_pyclbr.py", line 39, in assertHaskey
self.assertIn(key, obj)
~~~~~~~~~~~~~^^^^^^^^^^
AssertionError: 'Pdb' not found in {'contextmanager': <pyclbr.Function object at 0x000001DA2A910690>}
----------------------------------------------------------------------
Ran 6 tests in 11.022s
FAILED (failures=1, errors=1)

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.

Can you make it a local solution for Pdb then? possibly extracting this test into a separate function?

Comment threadLib/test/test_regrtest.py Outdated
Comment on lines 2551 to 2552
os.environ['PYTHONREGRTEST_UNICODE_GUARD'] = 'some_value' # for test_unicode_guard_env
unittest.main()

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.

Suggested change
os.environ['PYTHONREGRTEST_UNICODE_GUARD'] ='some_value'# for test_unicode_guard_env
unittest.main()
withEnvironmentVarGuard() asenv:
# for test_unicode_guard_env
PYTHONREGRTEST_UNICODE_GUARD="some_value"
unittest.main()

and add some from test.support.os_hepler import EnvironmentVarGuard top-level import. However, I don't know if this is correct to test test_unicode_guard_env like this or if we shouldn't just skip if it we're running it from __main__. Namely, decorate the test with @unittest.skipIf(__name__ == '__main__') cc @vstinner

@picnixzpicnixz changed the title gh-131290: fix direct test files invocationfix direct test files invocationMar 17, 2025
@picnixzpicnixz changed the title fix direct test files invocationgh-131290: fix direct test files invocationMar 17, 2025
@picnixz

Copy link
Copy Markdown
Member
  • The CLA needs to be signed.
  • The NEWS entry seems to fail but I don't know if it's because of its name or if it's because of its content.

@vstinner

Copy link
Copy Markdown
Member

For regrtest, I would prefer this fix:

diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
index 510c8f69631..7e317d5ab94 100644
--- a/Lib/test/test_regrtest.py+++ b/Lib/test/test_regrtest.py@@ -2546,4 +2546,5 @@ def test_test_result_get_state(self):
if __name__ == '__main__':
+ setup.setup_process()
unittest.main()

picnixz
picnixz previously requested changes Mar 30, 2025
Comment threadLib/test/test_metaclass.py
Comment threadLib/test/test_pyclbr.py Outdated
if __name__ == "__main__":
import importlib.util

# Adding the __spec__ attribute to the __main__ module

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.

Can you make it a local solution for Pdb then? possibly extracting this test into a separate function?

Comment threadMisc/NEWS.d/next/Tests/2025-03-17-19-47-27.gh-issue-131290.NyCIXR.rst Outdated
@bedevere-app

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Comment threadMisc/NEWS.d/next/Tests/2025-04-07-13-45-23.gh-issue-131290.MaximGit1.rst Outdated
@bedevere-app

Copy link
Copy Markdown

Thanks for making the requested changes!

: please review the changes made to this pull request.

@picnixz
picnixz self-requested a review April 6, 2025 22:46
Comment threadLib/test/test_pyclbr.py Outdated
Comment threadLib/test/test_pyclbr.py Outdated
Comment threadLib/test/test_pyclbr.py Outdated
Comment threadLib/test/test_pyclbr.py Outdated

@picnixzpicnixz 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.

I'm not on my dev session so I can't check but can you check again that the entire test suite is now correctly runnable as standalone files? TiA.

Comment threadLib/test/test_pyclbr.py Outdated
compare(None, actual, None, expected)

def test_pdb_module(self):
with temporary_main_spec():

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.

With the context manager, it's maybe better to actually put this one back in test_others(). We're also already using with warnings.catch_warnings(). Sorry for this oversight!

deftest_others(self):
cm= ...
...
withtemporary_main_spec():
cm('pdb', ...)

@MaximGit1

Copy link
Copy Markdown
ContributorAuthor

ok i checked the tests here are the results

./python .\Lib\test\test_metaclass.py
Running Debug|x64 interpreter...
.
----------------------------------------------------------------------
Ran 1 test in 0.191s
OK
./python .\Lib\test\test_pyclbr.py
Running Debug|x64 interpreter...
......
----------------------------------------------------------------------
Ran 6 tests in 10.211s
OK
./python .\Lib\test\test_regrtest.py Running Debug|x64 interpreter...
...................................................................................................................
----------------------------------------------------------------------
Ran 115 tests in 242.723s
OK

Comment threadLib/test/test_pyclbr.py Outdated
Comment threadLib/test/test_pyclbr.py
Comment threadLib/test/test_pyclbr.py
Comment threadLib/test/test_pyclbr.py
Comment threadLib/test/test_pyclbr.py Outdated
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@picnixz

Copy link
Copy Markdown
Member

I'm sorry I forgot about this one before leaving! I'll merge this by Sunday!

@picnixz
picnixz enabled auto-merge (squash) April 12, 2025 07:24
@picnixz
picnixz disabled auto-merge April 12, 2025 07:24
@picnixz
picnixz enabled auto-merge (squash) April 12, 2025 07:25
@picnixzpicnixz changed the title gh-131290: fix direct test files invocationgh-131290: ensure that test files can be executed as standalone scriptsApr 12, 2025
@picnixz
picnixz merged commit 292a724 into python:mainApr 12, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testsTests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@MaximGit1@picnixz@vstinner