Skip to content

#3823 Fix trace option for unittest test cases #4083

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Ben Webb
Benjamin Peterson
Bernard Pratz
Bob Ippolito
Brian Choi (bkjchoi72)
Brian Dorsey
Brian Maissy
Brian Okken
Expand Down
1 change: 1 addition & 0 deletions changelog/3823.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed the ``--trace`` option so that it works with unittest test cases.
2 changes: 2 additions & 0 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ def _handle_skip(self):
return False

def runtest(self):
if self.config.pluginmanager.get_plugin("pdbtrace"):
Copy link
Member

Choose a reason for hiding this comment

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

something about this change sits very wrong with me i will require a deeper review of the interaction, cause it just looks "wrong" - it cant be right to just invoke the hook that way

Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt Oct 5, 2018

Choose a reason for hiding this comment

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

after tracing the code for the trace plug-in and the debugging plug-in as well as the unittest plug-in, i believe this code will trigger double test execution when tracing

Copy link
Author

@bkjchoi72 bkjchoi72 Oct 5, 2018

Choose a reason for hiding this comment

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

Thanks for the insight @RonnyPfannschmidt . Could you elaborate on why or how this triggers double test execution, how I can reproduce it, and possibly some ideas on how I can go about invoking the hook in a different way?

Copy link
Member

Choose a reason for hiding this comment

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

the double code execution happens due to in addition to running the normal test code below, when tracing calling the hook

i believe calling the hook itself is not fitting the intent of the unittest integration (as we explicitly invoke the testcase machinery of unittest for setup/teardown behaviour)

so in addition of the double test call, i believe the pyfunc_call based code additionally also skips the unittest setup/teardown machinery for the per test "scope"

to be honest without a own deeper investigation i have no idea where to actually hook in to make this "right"

Copy link
Author

Choose a reason for hiding this comment

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

Got it. Let me play with this over the weekend and see if I can come up with something.

Copy link
Author

Choose a reason for hiding this comment

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

@RonnyPfannschmidt I want to clarify the behavior of the trace plugin when the test case has a setUp() method. Let's say I have the following test case:

1 import unittest
2 class MyTest(unittest.TestCase):
3     def setUp(self):
4         assert True
5     def test_1(self):
6         assert True

When you run pytest with trace option, should this break on line 4, or line 6?

What I find interesting is that if we have a pytest function with a fixture, the break starts at the function, not the fixture. For example, let's say we have the following test:

1 import pytest
2 @pytest.fixture
3 def setup():
4     assert True
5 def test_1(setup):
6     assert True

Running pytest with trace option will break at line 6. If we want to be consistent, running the trace option on a unittest style test case should break at the actual test, not the setup. Is this the correct behavior?

Copy link
Member

Choose a reason for hiding this comment

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

yes, break at the start of the test is the intended behaviour

self.ihook.pytest_pyfunc_call(pyfuncitem=self)
if self.config.pluginmanager.get_plugin("pdbinvoke") is None:
self._testcase(result=self)
else:
Expand Down
18 changes: 18 additions & 0 deletions testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,21 @@ def test_1():
assert "1 passed" in rest
assert "reading from stdin while output" not in rest
TestPDB.flush(child)

def test_unittest_using_trace_invokes_pdb(self, testdir):
p1 = testdir.makepyfile(
"""
import unittest
class Test1(unittest.TestCase):
def test_1(self):
assert True
"""
)
child = testdir.spawn_pytest("--trace " + str(p1))
child.expect("test_1")
child.expect("(Pdb)")
child.sendeof()
rest = child.read().decode("utf8")
assert "1 passed" in rest
assert "reading from stdin while output" not in rest
TestPDB.flush(child)