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

expose way to count examples in doctest.DocFileSuite() #60142

Open
cjerdonek opened this issue Sep 13, 2012 · 2 comments
Open

expose way to count examples in doctest.DocFileSuite() #60142

cjerdonek opened this issue Sep 13, 2012 · 2 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@cjerdonek
Copy link
Member

BPO 15938
Nosy @cjerdonek

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 = None
created_at = <Date 2012-09-13.11:12:39.186>
labels = ['type-feature', 'library']
title = 'expose way to count examples in doctest.DocFileSuite()'
updated_at = <Date 2012-09-13.13:48:13.593>
user = 'https://github.com/cjerdonek'

bugs.python.org fields:

activity = <Date 2012-09-13.13:48:13.593>
actor = 'chris.jerdonek'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2012-09-13.11:12:39.186>
creator = 'chris.jerdonek'
dependencies = []
files = []
hgrepos = []
issue_num = 15938
keywords = []
message_count = 2.0
messages = ['170430', '170442']
nosy_count = 1.0
nosy_names = ['chris.jerdonek']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue15938'
versions = ['Python 3.4']

@cjerdonek
Copy link
Member Author

This issue is to add to the doctest module an easy way to obtain the number of doctest examples in a unittest.TestSuite instance returned by a call to the doctest.DocFileSuite() function.

The unittest.TestSuite class currently exposes a countTestCases() method that gives the number of TestCase instances inside a test suite.

However, this method isn't useful for determining how many doctest examples that the TestSuite instance returned by a call to DocFileSuite() contains, nor in particular whether the instance contains any actual tests. Calling countTestCases() on the return value returns the number of files that were parsed and not the number of doctest examples.

In fact, getting the number of doctest examples contained in such a TestSuite is surprisingly obscure and seems to rely on implementation details and accessing private attributes. Here is one way to do it (annotated with comments):

    paths = ['Doc/howto/ipaddress.rst', 'Doc/howto/unicode.rst']

    # The return value of DocFileSuite is a unittest.TestCase instance, and
    # each item in the return value is a doctest.DocFileCase instance (which
    # in turn is also a doctest.DocTestCase and unittest.TestCase instance).
    suite = doctest.DocFileSuite(*paths, module_relative=False)

    total = 0
    for case in suite:
        doc_test = case._dt_test  # a doctest.DocTest instance.
        total += len(doc_test.examples)

    print("total test cases:  %s" % suite.countTestCases())
    print("total examples:   %s" % total)

Ideally, you would just be able to do something like this instead:

    suite.countExamples()

Exposing a method like that would probably involve converting DocFileSuite from a simple function that returns a unittest.TestSuite into a class that subclasses unittest.TestSuite (so that the function becomes a constructor).

@cjerdonek cjerdonek added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 13, 2012
@cjerdonek
Copy link
Member Author

The return value of DocFileSuite is a unittest.TestCase instance, and

s/unittest.TestCase/unittest.TestSuite/ (as elsewhere in the comment)

@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
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

1 participant