473,724 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doctest, unittest, or if __name__='__mai n__'

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 "if __name__ = '__main__':" at the bottom of your
module containing code to manually run your class through its paces.

So, which way is preferred? Seems to me that unittest is the way to go,
at least because it has you separate your test code from the code being
tested.

If unittest is the standard way to write test code, why do we still
have doctest? (I notice there's no mention in PEP 3000 of deprecating
the doctest module).

Mar 21 '06 #1
24 3091
jo***********@y ahoo.com wrote
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 "if __name__ = '__main__':" at the bottom of your
module containing code to manually run your class through its paces.

So, which way is preferred? Seems to me that unittest is the way to go,
at least because it has you separate your test code from the code being
tested.

If unittest is the standard way to write test code, why do we still
have doctest?
because it's a vastly superior way to write tests ?
(I notice there's no mention in PEP 3000 of deprecating
the doctest module).


why is it that comp.lang.pytho n is suddenly full of folks who
want to deprecate anything they don't understand ? don't
you have better things to do with your time ?

</F>

Mar 21 '06 #2
jo***********@y ahoo.com wrote:
If unittest is the standard way to write test code, why do we still
have doctest? (I notice there's no mention in PEP 3000 of deprecating
the doctest module).


Because both have their pros and cons and their right to exist. Doctest
is really easy to use and you can kill two birds with one stone.

Rather than removing one of the two, I would like to see yet another
alternatise such as py.test in the standard lib, because unittest is
indeed standard, but clumsy and un-pythonic.

-- Christoph

Mar 21 '06 #3
> because it's a vastly superior way to write tests ?

Well, since this is a mailing list where this sort of thing is
discussed, and since I was was asking about which way
is preferred, could you tell us why you (presumably) think
doctest is vastly superior to unittest?
why is it that comp.lang.pytho n is suddenly full of folks who
want to deprecate anything they don't understand ?
I didn't say I wanted to deprecate it. I wrote that I noticed
there was no mention in PEP 3k about deprecating it.
don't you have better things to do with your time ?


That wasn't very friendly.

Anyhow, I'm not attacking doctest, but rather, just trying
to understand why there seems to be two very similar ways
of testing your code built into the system (considering that
one motto around here is "There should be one -- and
preferably only one -- obvious way to do it.").

Mar 21 '06 #4
jo***********@y ahoo.com a écrit :
For writing testcode, it looks like there's three ways that it's
typically done:
Actually, there's at least one more
(1). using the doctest module,

(2). using the unittest module (i.e. "pyunit"), or else

(3). just putting an "if __name__ = '__main__':" at the bottom of your
module containing code to manually run your class through its paces.
(4) using pytest
So, which way is preferred? Seems to me that unittest is the way to go,
at least because it has you separate your test code from the code being
tested.
Which may or may not be such a good thing... I prefer to have my tests
with the code they're testing.
If unittest is the standard way to write test code, why do we still
have doctest?
Because doctest is simple, great, and definitively more pythonic than
unittest ?
(I notice there's no mention in PEP 3000 of deprecating
the doctest module).

Hopefully not.
Mar 21 '06 #5
> and you can kill two birds with one stone.

By that, do you mean you can write your tests and your
docstrings in one shot with doctest?
I would like to see yet another alternatise such as py.test
in the standard lib,


Ah: http://codespeak.net/py/current/doc/home.html

Mar 21 '06 #6
[jo***********@y ahoo.com <jo***********@ yahoo.com>[
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 "if __name__ = '__main__':" at the bottom of your
module containing code to manually run your class through its paces.

So, which way is preferred?
Serious testers often use both #1 and #2, depending on the specific
test at hand. #3 doesn't work well in practice.
Seems to me that unittest is the way to go, at least because it has
you separate your test code from the code being tested.
Equally true of #1, if you write doctest files separate from your
code. I suggest you look at Zope3, which is a major project using
that style extensively. In fact, it's standard practice there to
write "tutorial doctest" files for a new feature before writing code
to implement it. The text serves as a tutorial introduction to the
feature, and the tests scattered throughout clarify, illustrate, and
verify the text's claims. By writing the text in ReST format, these
doctest files also serve as the source for auto-generated
documentation in HTML format (available interactively from a Zope3
console). This combination of virtues has proved extremely
productive.

BTW, doctest makes it a little easier to write prose than to write
Python code, and your typical frazzled programmer is highly influenced
by small biases ;-) The reverse is true of raw unittest, which goes a
long way toward explaining why you see so many unittests in real life
that are incomprehensibl e: when it's easier to write code than
explanations, that's what people do.
If unittest is the standard way to write test code, why do we still
have doctest?
Because doctest is much better for some kinds of testing. See above.
(I notice there's no mention in PEP 3000 of deprecating
the doctest module).


Or of deprecating the unittest module ;-)
Mar 21 '06 #7
jo***********@y ahoo.com schrieb:
Anyhow, I'm not attacking doctest, but rather, just trying
to understand why there seems to be two very similar ways
of testing your code built into the system (considering that
one motto around here is "There should be one -- and
preferably only one -- obvious way to do it.").


I don't think doctest and unittest are similar ways; they are pretty
different ways. Also, the "one obvious way" should not be overstressed
and taken as a dogma for libraries, modules and frameworks. Actually, on
that level we see the opposite in the Python world: A plethora of tools
for XML processing, GUI frameworks, Web frameworks etc. Some think this
is deplorable, but I see this as something exciting and positive.

-- Christoph
Mar 21 '06 #8
jo***********@y ahoo.com wrote:
By that, do you mean you can write your tests and your
docstrings in one shot with doctest?


Right. The tests serve also as usage examples.
Mar 21 '06 #9
On Tue, 21 Mar 2006 22:26:57 +0100
Christoph Zwerschke <ci**@online.de > wrote:
jo***********@y ahoo.com wrote:
If unittest is the standard way to write test code, why
do we still have doctest? (I notice there's no mention
in PEP 3000 of deprecating the doctest module).


Because both have their pros and cons and their right to
exist. Doctest is really easy to use and you can kill two
birds with one stone.

Rather than removing one of the two, I would like to see
yet another alternatise such as py.test in the standard
lib, because unittest is indeed standard, but clumsy and
un-pythonic.


e.g....
Doctest is very easy to use, so it's easy to ensure that
tests get written. OTOH, they're only really good for stuff
that can be easily tested in the interpreter (e.g. that can
be easily verified from their text output). When you get
into stickier stuff like graphics and web programming, the
formal structure of pyunit can be easier to adapt than
something which is intrinsically based on string processing.

Haven't seen py.test before, but I'm looking now -- thanks
for the link. :-)

Cheers,
Terry

--
Terry Hancock (ha*****@Anansi Spaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 21 '06 #10

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

Similar topics

1
11454
by: Eli Stevens \(WG.c\) | last post by:
I have a question about proper Python style when it comes to having a main function in a module. I'm fairly new to Python - a few months of very-part-time tinkering (lots'o'Java at work, shrug); my apologies if this has been hashed out before. Random Googling didn't enlighten me, so instead I'll ask here. :) Take the following strmod.py (it's silly, I know): ---- import sys import getopt
0
2054
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally TestCase.skipIf(expr, msg): skips if expr is true These can be called either in setUp() or in the test methods. I also added reporting of skipped tests to TestResult, _TextTestResult and
5
2751
by: Ed Leafe | last post by:
I'm working on creating a generic runtime engine for the Dabo framework. Right now I'm focusing on Windows, since many of our potential users are running on that platform. I've got py2exe and Inno Setup running, so all that is well and good. My question concerns the ability to generically run scripts as if they were being run on an installed copy of Python. Many scripts have the following structure: if __name__ == "__main__":
7
2073
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good old lines at the end: if __name__ == "__main__": unittest.main() This is great, because each of the modules are runnable, and I can select classes or tests to run on the commandline if I want to. However, running all the tests from e.g. a...
4
1482
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 "displeje_pokus.txt" (you can see the simplified contents of the files bellow). When I run "python displeje_pokus.py" I am getting an error (see
20
4196
by: benhoyt | last post by:
Hi guys, I've been using Python for some time now, and am very impressed with its lack of red tape and its clean syntax -- both probably due to the BDFL's ability to know when to say "no". Most of the things that "got me" initially have been addressed in recent versions of Python, or are being addressed in Python 3000. But it looks like the double underscores are staying as is. This is probably a good thing unless there are better...
3
1971
by: Paul Moore | last post by:
My normal testing consists of a tests.py script using unittest, with the basic if __name__ == '__main__': unittest.main() incantation to get things going. But I now want to incorporate some other tests (specifically, a text file containing doctests) and I find that there is no gradual process
0
8868
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8741
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9243
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9160
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4771
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.