DocTest unittest discovery adapter #24601
Labels
feature-request
Request for new features or functionality
info-needed
Issue requires more information from poster
triage-needed
Needs assignment to the proper sub-team
I have looked at previous issues and feature requests but have found no mention of this problem. Please let me know if I missed something or have made a mistake below.
Behaviour
When using both unittest tests and doctests in a project the official python docs recommend adapting the doctests into the unittest discovery process, as you can see here:
This allows us to run doctests systematically along with regular unit tests.
Under the hood this adapter creates doctest.DocTestCase instances, a subclass of unittest.TestCase. This is not a publicly documented class, but exploring the source we can see that it overrides the
id()
method to point to the underlying doctest.DocTest instance'sname
attribute, which in turn is the qualified name of the object that contains the doctest (could be a module, a class or a function).In vscode-python's unittest adapter script
unittest.TestCase.id()
is used to build the test tree. This means that doctests discovered in the way described above are displayed "incorrectly" in the test explorer tab:*folders, filename, class_name, function_name
in the id, doctest trees are only correct if the doctest is on a member function of a class. Module and class level doctests have a tree view where a random component of their module name is labeled.py
. For example a doctest on a class with a qualified name ofsome.path.to.module.SomeClass
would have a tree ofTo be clear all other functionality of these tests work, they can be run from the test explorer and they report their results correctly. Only the tree and Go to test don't work.
Solution
I think a minor change of creating a special case for doctest.DocTestCase instances in
build_test_tree
could solve all of these issues. As mentioned doctest.DocTestCase instances have a_dt_test
attribute which holds a reference to the underlying doctest.DocTest. This has both afilename
andlineno
attribute and all necessary information is present to build a correct tree.I have not researched how the gutter icon is displayed in the editor, but could such a change also automatically fix those?
I'd be happy to take a crack at a PR if possible.
The text was updated successfully, but these errors were encountered: