Skip to content

Fixed #27391 -- Implemented SimpleTestCase.debug(). #7436

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

Merged
merged 1 commit into from
Oct 18, 2019

Conversation

asfaltboy
Copy link
Contributor

https://code.djangoproject.com/ticket/27391

Users calling debug() expect it to bubble up an exception if occurring in test, but behave the
same as run() when no exception occurred.

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch from bb410ca to 770250d Compare October 26, 2016 22:48
@timgraham
Copy link
Member

Did you consider trying to factor the shared logic with __call__() to avoid the repetition?

@asfaltboy
Copy link
Contributor Author

asfaltboy commented Oct 28, 2016

I was really trying to avoid it, I'd hate to see current simplicity get bogged down... but, how about this:

def _setup_and_call(self, result=None, debug=False):
    """
    Wrapper around default __call__ method to perform common Django test
    set up. This means that user-defined Test Cases aren't required to
    include a call to super().setUp().

    If passed debug=True, does does not catch errors and calls super().debug
    instead of __call__.
    """

    testMethod = getattr(self, self._testMethodName)
    skipped = (
        getattr(self.__class__, "__unittest_skip__", False) or
        getattr(testMethod, "__unittest_skip__", False))

    if not skipped:
        try:
            self._pre_setup()
        except Exception:
            result.addError(self, sys.exc_info())
            if debug:
                raise
            return
    if debug:
        super(SimpleTestCase, self).debug()
    else:
        super(SimpleTestCase, self).__call__(result)

    if not skipped:
        try:
            self._post_teardown()
        except Exception:
            result.addError(self, sys.exc_info())
            if debug:
                raise
            return

We can refactor this into smaller components obviously, if you prefer.

Update: simplified by removing contextmanager, it's actually shorter and avoids a bug where test is executed even if setup failed.

Update2: also found and amended an issue with running the skip test since in debug it raised SkipTest. Pushed again to run test suite.

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch 2 times, most recently from d1f2eae to cc1c99a Compare October 31, 2016 08:48
@asfaltboy
Copy link
Contributor Author

asfaltboy commented Oct 31, 2016

@timgraham please take a look at the updated code. Is this more or less what you envisioned? Or would you like anything changed here?

Also, should we mention this in docs/changelog ?

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch from cc1c99a to a98dcfa Compare October 31, 2016 12:05
@timgraham timgraham force-pushed the 27391-support-testcase-debug branch from a98dcfa to 71f86c3 Compare February 6, 2017 20:53
Copy link
Member

@timgraham timgraham left a comment

Choose a reason for hiding this comment

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

I pushed a few updates -- some clean up remains if you could polish. Try to split the tests up rather than having one bit test. The tests that don't involve debug() are probably already passing before this change and so could be added in a separate commit that precedes the commit that adds debug().

@timgraham
Copy link
Member

@asfaltboy, do you have any time and interest in completing this soon?

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch 2 times, most recently from b32f154 to 0de4393 Compare June 20, 2017 21:16
@asfaltboy
Copy link
Contributor Author

Hi @timgraham sorry for taking long to respond (and for hogging the CI machine with amends).

How does the test case look now?

@timgraham
Copy link
Member

It looks like there's a test isolation problem (see the build failures).

@asfaltboy
Copy link
Contributor Author

Thanks @timgraham I'll try to address it later today or this week. Time constraints, sorry ...

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch from 71cd053 to 40f0517 Compare September 18, 2017 09:43
@asfaltboy
Copy link
Contributor Author

asfaltboy commented Sep 18, 2017

@timgraham it took "a bit" longer than promised, but I finally got to it.

The isolation issue was due to raised exception in Debug requiring to properly cleaning up after we're done with suite.

It's not a coincidence, because this is exactly what the fix intends to do, to let the test running framework (e.g pytest) to be able to catch errors and do their own cleanup as desired.

@timmartin
Copy link
Contributor

Other than the exception issue above, this looks good to me. Perhaps this deserves an entry in the release notes?

@asfaltboy
Copy link
Contributor Author

asfaltboy commented Dec 10, 2017

@timmartin I added a commit & comment for the mentioned case.

About updating release notes, I think we can rebase this PR over latest master and add this under 2.0.txt: Miscellaneous (currently line 634):

+* Implement the ``SimpleTestCase.debug()`` method to allow a test to run without
+  collecting the result. This allows exceptions raised by the test to be propagated to
+  the caller, and is used to support running tests under a debugger.
+  (:ticket:`27391`).

Copy link
Member

@timgraham timgraham left a comment

Choose a reason for hiding this comment

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

Test coverage seems lacking as I commented out the two

if debug:
    raise

blocks and don't see any test failures.

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch from 4bad9e7 to bbfcf5b Compare July 26, 2018 16:27
@asfaltboy
Copy link
Contributor Author

Hi @timmartin @timgraham , I've had some time during the europython event to address the requested changes. I've had to move the addError call to under the raise call in both pre-set and post-teardown clauses (which makes sense) and added tests for those.

The sqlite3-windows tests were failing on CI so I rebased (maybe un-necessarily) and now the sqlite3-bionic test is failing. Is this unrelated?

@timgraham
Copy link
Member

Now I can comment both result.addError(self, sys.exc_info()) without seeing any test failures.

Yes, the build failure is unrelated. Don't worry about that.

Copy link
Contributor

@LuRsT LuRsT left a comment

Choose a reason for hiding this comment

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

Looks good, can we get this merged @Ian-Foote

@LilyAcorn
Copy link
Contributor

This probably needs rebasing first, so it can be re-tested.

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch from bbfcf5b to be50796 Compare September 22, 2019 07:15
@asfaltboy
Copy link
Contributor Author

@Ian-Foote @LuRsT thanks for your interest in merging this PR, it motivated me to get back to it.

I went ahead and rebased over latest master, and addressed the missing tests mentioned by @timgraham. These tests validate that the existing TestCase behavior, of capturing and logging errors in pre_setup & post_teardown, is retained after this change. (waiting for CI to confirm this runs correctly as tests fail on my machine fail erratically)

Is there anything we can address? Shall I squash the older WIP commits or leave them there? If nothing else, shall we mention this feature in release notes or docs?

@asfaltboy
Copy link
Contributor Author

The failing test seems to be unrelated as it failed to check out the "PR merge commit", maybe this is an issue with the git plugin like the one described here ?

@LilyAcorn
Copy link
Contributor

@carltongibson @felixxm

@felixxm
Copy link
Member

felixxm commented Sep 23, 2019

@asfaltboy Failing tests are related with force-push you don't need to worry about them.

It is a new feature so documentation (topics/testing/tools.txt) and release notes are required.

@asfaltboy
Copy link
Contributor Author

Thanks @felixxm, I doc'd the method in topics/testing/tools.txt and in releases/3.0.txt.

I feel, however, that this may be "too low-level" for the tools docs.
The target audience is Django test tool developers (pytest-django, nose, etc...), rather than Django users. I see this as the same reason that we don't document SimpleTestCase.run(), or am I wrong and we should have documented run() in the first place 🙂?

@asfaltboy asfaltboy force-pushed the 27391-support-testcase-debug branch 2 times, most recently from a56357c to 56e84d2 Compare September 30, 2019 12:31
@felixxm
Copy link
Member

felixxm commented Oct 18, 2019

@asfaltboy I moved extra tests to a separate PR #11936.

@felixxm felixxm force-pushed the 27391-support-testcase-debug branch from 56e84d2 to f98d2ec Compare October 18, 2019 10:18
@felixxm felixxm changed the title Fixed #27391 -- Implement SimpleTestCase.debug method Fixed #27391 -- Implemented SimpleTestCase.debug(). Oct 18, 2019
@felixxm
Copy link
Member

felixxm commented Oct 18, 2019

@asfaltboy Thanks for updates 👍 I rebased from master, squashed commits. removed the SimpleTestCase.debug() documentation, added a versionchanged admonition, updated release notes and pushed minor edits. It's ready 🚀

debug() should bubbled up exceptions if occurring in test, but behave
the same as run() when no exceptions occurred.
@felixxm felixxm force-pushed the 27391-support-testcase-debug branch from f98d2ec to 1711c50 Compare October 18, 2019 10:23
@felixxm felixxm merged commit 1711c50 into django:master Oct 18, 2019
blueyed added a commit to blueyed/pytest-django that referenced this pull request Oct 18, 2019
blueyed added a commit to pytest-dev/pytest-django that referenced this pull request Oct 18, 2019
beyondgeeks pushed a commit to beyondgeeks/django-pytest that referenced this pull request Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants