Skip to content

bpo-42722: Add --debug command line option to unittest to enable post-mortem debugging #23900

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Dominik1123
Copy link
Contributor

@Dominik1123 Dominik1123 commented Dec 22, 2020

Copy link
Contributor

@ZackerySpytz ZackerySpytz left a comment

Choose a reason for hiding this comment

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

Please add a news entry.

Comment on lines +324 to +328
def test_debug(self):
program = self.program
program.testRunner = FakeRunner
program.parseArgs([None, '--debug'])
self.assertTrue(program.debug)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe more tests are needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean more tests for the argument parsing part? I've also added a general test that ensures that any exception is leaked for --debug rather than being consumed by unittest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ZackerySpytz What kind of tests do you think should be added? I'm happy to add more, but at the moment it's not clear to me what exactly should be tested in addition to what's already there.

Copy link
Contributor

Choose a reason for hiding this comment

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

Never mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ZackerySpytz I'm not familiar with the review process, what do I need to do to further advance this PR?

Dominik1123 and others added 3 commits December 24, 2020 02:16
@github-actions
Copy link

github-actions bot commented Feb 5, 2021

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Feb 5, 2021
Copy link
Contributor

@MaxwellDupre MaxwellDupre left a comment

Choose a reason for hiding this comment

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

I checked out this branch, fresh compile, ran:
cpython on  resolve-42722 [$?] via 🐍 v3.11.0a3+>
./python -m unittest --debug ../TestGeneric/main.py and

./python --debug ../TestGeneric/main.py but all I got was
module = __import__(module_name) ValueError: Empty module name
and
unknown option --debug usage: ./python [option] ... [-c cmd | -m mod | file | -] [arg] ... Try python -h for more information.
Where main.py is the test script from the bug.
Any ideas why or what I am doing wrong?

@Dominik1123
Copy link
Contributor Author

I checked out this branch, fresh compile, ran: cpython on  resolve-42722 [$?] via snake v3.11.0a3+

How did you compile exactly? To use the latest version, you'd have to check out this branch locally, then merge back the main dev branch and then build it.

./python --debug ../TestGeneric/main.py

When you invoke it like that, you're passing --debug as a command line option to the python executable, but Python doesn't support such an option. You'd have to use python ../TestGeneric/main.py --debug instead.

Where main.py is the test script from the bug.

What bug are you referring to? You mean the BPO issue?

@MaxwellDupre
Copy link
Contributor

On Github on my cpython fork: Fetch Upstream (and merge). Pull my fork to local with Github GUI.
On latop check cpython branch is main
sudo make clean # twice
./configure --with-pydebug
make -s
sudo make altinstall
I check which code has been changed; so for C code I will recompile from scratch on the PR branch, but in this case as only the py code has changed I will only do a gh pull and then test.
gh pr checkout 23900
Check terminal prompt shows correct branch such as:
cpython on  resolve-42722 [$?] via s v3.11.0a3+>
Basically I follow the dev guide on https://devguide.python.org/.

OK so I tried again with the last param as --debug and the output is:
./python ../TestGeneric/main.py --debug
101
102
Traceback (most recent call last):
File "/home/me/Documents/cpython/../TestGeneric/main.py", line 15, in
unittest.main()
^^^^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/main.py", line 103, in init
self.runTests()
^^^^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/main.py", line 276, in runTests
testRunner.debug(self.test)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/runner.py", line 16, in debug
test.debug()
^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/suite.py", line 138, in debug
self.run(debug, True)
^^^^^^^^^^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/suite.py", line 124, in run
test.debug()
^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/suite.py", line 138, in debug
self.run(debug, True)
^^^^^^^^^^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/suite.py", line 124, in run
test.debug()
^^^^^^^^^^^^
File "/home/me/Documents/cpython/Lib/unittest/case.py", line 657, in debug
getattr(self, self._testMethodName)()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/me/Documents/cpython/../TestGeneric/main.py", line 11, in test_foo
self.assertIs(foo(), None)
^^^^^
File "/home/me/Documents/cpython/../TestGeneric/main.py", line 6, in foo
print(x + 100)
~~^~~~~
TypeError: can only concatenate str (not "int") to str
cpython on  resolve-42722 [$?] via 🐍 v3.11.0a5+

Finally, yes the bpo.

@Dominik1123
Copy link
Contributor Author

Yes, I can confirm that, so it looks like it works: it's calling test.debug() according to the traceback. In order to use the debugger, you can invoke it as usual:

./python -m pdb ../TestGeneric/main.py --debug

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Aug 2, 2022
@kxrob
Copy link

kxrob commented Oct 22, 2022

I used this and a problem was that skipped tests broke the behavior with early crashes at SkipTest excepion. I made these changes so far to make it usable :

--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -653,9 +653,19 @@ def __call__(self, *args, **kwds):
 
     def debug(self):
         """Run the test without collecting errors in a TestResult"""
-        self.setUp()
-        getattr(self, self._testMethodName)()
-        self.tearDown()
+        print(self, "...", end=' ', file=sys.stderr)
+        try:
+            self.setUp()
+        except SkipTest:
+            print("SKIPPED", file=sys.stderr)
+        else:
+            try:
+                getattr(self, self._testMethodName)()
+            except SkipTest:
+                print("SKIPPED", file=sys.stderr)
+            else:
+                print("ok", file=sys.stderr)
+            self.tearDown()
         while self._cleanups:
             function, args, kwargs = self._cleanups.pop(-1)
             function(*args, **kwargs)

I'd also like to see the usual (verbose) progress and (in success case) final summary info, particularly on long test runs and with complex custom test loaders, so that this mode is usable as the primary mode in the first place during dev instead of the "dead" display only mode. Thus the regular result collection business in debug mode. Instead of void silence till end.

After all a debug / crash mode is of fundamental importance. Wonder why such thing is still not in.

@armw4

This comment was marked as spam.

@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA signed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants