473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

doctest redundant mock class instantiations

mde
I'm wondering if there is a "best practice" for *creating doctests in
methods* that reduces clutter/duplication of dummy instantiations. In
the following code there are five (labeled 1-5) possible places to put
a
dummy mock instantiation. I have the impression that creating the
dummies in every method is the common practice (1-3), but I'm hoping
otherwise. I've found that creating the dummy down by the testmod
call
obviates the need to do any of the other (1-5) instantiations.

Doctests are written so concisely in *functions* (x6), but they are
tedious in *methods* due the redundant creation of dummies. This
tedium
makes me prefer writing functions over methods, but I hate to
sacrifice on design.

It seems that __init__ is the most intuitive place to put a single
instantiation (#1), but alas, that doesn't work as it's not visible to
#2,3. Next best would be in the module-level docstring (#5); also no
dice. #2,3 are tedious. The #4 approach works okay; is there any
problem with adopting this (#4) approach as a convention? Is there a
better way, to make it more like what's possible in functions?

"""
>>mockSM = SillyMult() # 5
"""

class SillyMult(object):
def __init__(self):
"""
>>mockSM = SillyMult() # 1
"""
pass
def x2(self, x):
"""
>>mockSM = SillyMult() # 2
mockSM.x2(2)
4
"""
return x*2

def x3(self, x):
"""
>>mockSM = SillyMult() # 3
mockSM.x3(2)
6
"""
return x*3

def x6(x):
"""
>>x6(2) # 6
12
"""
return x*6

if __name__ == '__main__':
import doctest
#mockSM = SillyMult() # 4
doctest.testmod()
~

--
Micah Elliott | http://MicahElliott.blogspot.com | @MicahElliott
Oct 25 '08 #1
2 1576
On Fri, 24 Oct 2008 17:16:50 -0700, mde wrote:
I'm wondering if there is a "best practice" for *creating doctests in
methods* that reduces clutter/duplication of dummy instantiations. In
the following code there are five (labeled 1-5) possible places to put a
dummy mock instantiation. I have the impression that creating the
dummies in every method is the common practice (1-3), but I'm hoping
otherwise. I've found that creating the dummy down by the testmod call
obviates the need to do any of the other (1-5) instantiations.

Doctests are written so concisely in *functions* (x6), but they are
tedious in *methods* due the redundant creation of dummies. This tedium
makes me prefer writing functions over methods, but I hate to sacrifice
on design.

It seems that __init__ is the most intuitive place to put a single
instantiation (#1), but alas, that doesn't work as it's not visible to
#2,3. Next best would be in the module-level docstring (#5); also no
dice. #2,3 are tedious. The #4 approach works okay; is there any
problem with adopting this (#4) approach as a convention? Is there a
better way, to make it more like what's possible in functions?
You've missed one: in the class doc string itself:

class SillyMult(object):
"""Object that multiplies in a silly fashion.

SillyMult objects have silly hard-coded methods to multiply
by two:
>>mockSM = SillyMult()
mockSM.x2(2)
4

three:
>>mockSM.x3(2)
6

and even six:
>>x6(2)
12

"""
# implementation [snipped]

Personally, I tend to use a combination of approaches. Since doctests
aren't intended for full test coverage, I use *short* tests in methods.
If I can't make a test short, it doesn't go into the method doctest. For
more extensive tests, I put them into the class or even module doctest.

However, short doesn't necessarily mean "one line". In most of my
methods, the docstring is fairly detailed, explaining arguments,
exceptions raised, results returned, with three or four doc tests to
cover the usual cases. Adding an extra line to create a dummy instance is
no hardship.

--
Steven
Oct 25 '08 #2
On Sat, 25 Oct 2008 03:29:39 +0000, Steven D'Aprano wrote:
Personally, I tend to use a combination of approaches. Since doctests
aren't intended for full test coverage, I use *short* tests in methods.
If I can't make a test short, it doesn't go into the method doctest. For
more extensive tests, I put them into the class or even module doctest.
IMHO doctests in docstrings are not for testing the code but for testing
usage examples in the docstrings. So I don't write *tests* there, but
useful examples for the programmer who might have to use that functions
or methods and use the doctest module to check if my examples agree with
the actual implementation.

Ciao,
Marc 'BlackJack' Rintsch
Oct 25 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thomas Heller | last post by:
I've just read the 'Automating testing with doctest' pycon paper, and I'm exited about the future of doctest. This simple script reads the ctypes docs written in StructuredText, and runs and...
2
by: Alan G Isaac | last post by:
> python doctest.py -v Running doctest.__doc__ Trying: .remove(42) Expecting: Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: list.remove(x): x not in list ok...
2
by: Michele Simionato | last post by:
Some time ago I hacked a custom solution to run doctests on text files containing documentation. The solution involved this kind of game: tester=doctest.Tester(globs={},verbose=1)...
3
by: John J Lee | last post by:
Is it possible to get doctest-mode to work with mmm-mode and python-mode nicely so that docstrings containing doctests are editable in doctest-mode? In my utter e-lisp ignorance, I tried this: ...
1
by: Runsun Pan | last post by:
I intend to use the doctect heavily. For this I am thinking of coding a class that comes with a built-in doctest functionality. I'd like to seek for input before I start. The idea is to have a...
24
by: john_sips_tea | last post by:
For writing testcode, it looks like there's three ways that it's typically done: (1). using the doctest module, (2). using the unittest module (i.e. "pyunit"), or else (3). just putting an...
4
by: petr.jakes.tpc | last post by:
Hi, inspired by the article written by Tarek Ziade in the February 07 issue of the "Linux +" magazine I am experimenting with the doctest module. I have two files, "displeje_pokus.py" and...
0
by: Fuzzyman | last post by:
Mock 0.4.0 has just been released, the first release in about ten months (but worth the wait). Mock is a simple library for testing: specifically for mocking, stubbing and patching. * Mock...
0
by: dj | last post by:
Hello, I have just started working with minimock in doctest. I want to create a mock pyodbc object which returns a string value when the method execute is called. Here is my doctest: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.