Skip to content
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

doctest.DocTestCase fails when run repeatedly #46856

Closed
PiDelport mannequin opened this issue Apr 10, 2008 · 15 comments
Closed

doctest.DocTestCase fails when run repeatedly #46856

PiDelport mannequin opened this issue Apr 10, 2008 · 15 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@PiDelport
Copy link
Mannequin

PiDelport mannequin commented Apr 10, 2008

BPO 2604
Nosy @mgedmin, @amauryfa, @PiDelport, @JelleZijlstra, @miss-islington, @iritkatriel, @DanielNoord
PRs
  • bpo-2604: Make DocTestCase reset globs in teardown #31932
  • [3.9] bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932) #32057
  • [3.10] bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932) #32058
  • Files
  • reset_globs.patch: Zope's fix, plus a test docstring tweak
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-03-23.02:02:51.156>
    created_at = <Date 2008-04-10.03:40:45.799>
    labels = ['easy', 'type-bug', '3.9', '3.10', '3.11', 'library']
    title = 'doctest.DocTestCase fails when run repeatedly'
    updated_at = <Date 2022-03-29.09:00:00.014>
    user = 'https://github.com/PiDelport'

    bugs.python.org fields:

    activity = <Date 2022-03-29.09:00:00.014>
    actor = 'PiDelport'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-03-23.02:02:51.156>
    closer = 'JelleZijlstra'
    components = ['Library (Lib)']
    creation = <Date 2008-04-10.03:40:45.799>
    creator = 'PiDelport'
    dependencies = []
    files = ['10060']
    hgrepos = []
    issue_num = 2604
    keywords = ['patch', 'easy']
    message_count = 15.0
    messages = ['65285', '65286', '65335', '65339', '65344', '65345', '65346', '167696', '415090', '415339', '415812', '415817', '415818', '415849', '416248']
    nosy_count = 9.0
    nosy_names = ['jamesh', 'mgedmin', 'amaury.forgeotdarc', 'PiDelport', 'python-dev', 'JelleZijlstra', 'miss-islington', 'iritkatriel', 'danielnoord']
    pr_nums = ['31932', '32057', '32058']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue2604'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @PiDelport
    Copy link
    Mannequin Author

    PiDelport mannequin commented Apr 10, 2008

    DocTestCase.tearDown destructively clears its DocTest instance's globs,
    preventing the test from being run repeatedly (such as with trial
    --until-failure).

    There's a fix for this in zope.testing's version of doctest, which
    resets the globs instead:

    http://svn.zope.org/?view=rev&rev=39023

    @PiDelport PiDelport mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 10, 2008
    @PiDelport
    Copy link
    Mannequin Author

    PiDelport mannequin commented Apr 10, 2008

    Addendum: This appears to be a regression in r36809 (Python 2.4+).

    @jamesh
    Copy link
    Mannequin

    jamesh mannequin commented Apr 11, 2008

    Is repeating a test with the same TestCase instance ever safe? It'd be
    better to create a new instance and run that.

    If any of the variables in test.globs are changed by the test (e.g.
    appending to a list), then rerunning the test will not necessarily give
    the same result.

    While the zope change allows tests that have immutable globals or don't
    change their globals to function, it also lets other tests "almost"
    work, and could lead to new bugs that are difficult to track down.

    @PiDelport
    Copy link
    Mannequin Author

    PiDelport mannequin commented Apr 11, 2008

    If any of the variables in test.globs are changed by the test (e.g.
    appending to a list), then rerunning the test will not necessarily give
    the same result.

    This is true, but modifying the globals such that subsequent runs of the
    same test can break equally affects subsequent runs of any other tests
    that use that module: such a test is already broken (unsafe to run with
    other tests) to begin with, independent of the DocTestCase.tearDown issue.

    @jamesh
    Copy link
    Mannequin

    jamesh mannequin commented Apr 11, 2008

    If I create a test case with a command like:

       test = DocFileSuite('foo.txt', globs={'somelist': [42]})

    The doctest isn't doing anything wrong if it modifies somelist.

    Furthermore, Glyph has said he thinks the current --until-failure
    behaviour in trial is a mistake: http://glyf.livejournal.com/72505.html

    @PiDelport
    Copy link
    Mannequin Author

    PiDelport mannequin commented Apr 11, 2008

    Well, whether that code is wrong depends on whether your project policy
    wants repeatable tests or not. A repeatable and arguably more idiomatic
    way of writing that example is to give DocFileSuite a setUp function
    which initializes any special globals required by the test.

    In any case, DocTestCase allowing non-repeatable tests (which i don't
    think are common) is no reason to disallow repeatable tests, which e.g.
    at least Zope considers important enough to motivate fixing this issue.
    (Zope is also the source of the code being fixed, if i'm not mistaken.)

    @amauryfa
    Copy link
    Member

    FWIW, the python testsuite needs repeatable tests, when running in
    "reference leaks" mode.

    See also r62100, where a DocTestSuite construction had to be moved into
    the repeated function.

    @mgedmin
    Copy link
    Mannequin

    mgedmin mannequin commented Aug 8, 2012

    For the record, this bug also breaks zope.testrunner's --repeat option, if you have any doctests in your test suite that rely on test.globs not going away.

    @iritkatriel iritkatriel added 3.9 only security fixes 3.10 only security fixes labels Nov 1, 2020
    @iritkatriel
    Copy link
    Member

    I've reproduced this on 3.11 as well.

    The patch here needs to be converted to a GitHub PR and then tested and reviewed.

    The patch on bpo-9736 has a unit test as well, which should be included (because the pjd's patch doesn't have one).

    @iritkatriel iritkatriel added easy 3.11 only security fixes labels Mar 13, 2022
    @DanielNoord
    Copy link
    Mannequin

    DanielNoord mannequin commented Mar 16, 2022

    I have created a PR that combines the patch here with the patch from bpo-9736.

    @JelleZijlstra
    Copy link
    Member

    New changeset 7ba7eae by Daniël van Noord in branch 'main':
    bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932)
    7ba7eae

    @miss-islington
    Copy link
    Contributor

    New changeset 3c60190 by Miss Islington (bot) in branch '3.9':
    bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932)
    3c60190

    @miss-islington
    Copy link
    Contributor

    New changeset f163ad2 by Miss Islington (bot) in branch '3.10':
    bpo-2604: Make doctest.DocTestCase reset globs in teardown (GH-31932)
    f163ad2

    @JelleZijlstra
    Copy link
    Member

    After 14 years this bug is finally fixed. Thanks everyone for the patches and discussion.

    @PiDelport
    Copy link
    Mannequin Author

    PiDelport mannequin commented Mar 29, 2022

    Thanks! 😄️

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants