[Python-bugs-list] [Bug #131480] __test__() should auto-exec at compile time

noreply@sourceforge.net noreply@sourceforge.net
Sat, 10 Feb 2001 20:18:30 -0800


Bug #131480, was updated on 2001-Feb-07 18:44
Here is a current snapshot of the bug.

Project: Python
Category: None
Status: Closed
Resolution: Wont Fix
Bug Group: Feature Request
Priority: 5
Submitted by: justinshaw
Assigned to : jhylton
Summary: __test__() should auto-exec at compile time

Details: We can make unit testing as simple as writing the test code!
Everyone agrees that unit tests are worth while.  Python does a great job
removing tedium from the job of the programmer.  Unit test should run
automatically.  Here's a method everyone can agree to:

Have the compiler check each module for a funtion with the special name
'__test__' that takes no arguments.  If it finds it it calls it.

The problem of unit testing divides esiliy into two pieces: How to create
the code and how to execute the code.  There are many options in creating
the code but I have never seen any nice solutions to run the code
automatically
"if __name__ == '__main__':"
 doesn't count since you have to do somthing special to call the code i.e.
run it as a script.  There are of course ways to run the test code
automatically but the ways I have figured out run it on every import (way
too often especially for long tests).  I imagine there is a way to check to
see if the module is loaded from a .pyc file and execute test code
accouringly but this seems a bit kludgy.  Here are the benifits of compile
time auto-execution:

- Compatible with every testing framework.
- Called always and only when it needs to be executed.
- So simple even micro projects 'scripts' can take advantage

Disadvantages:
- Another special name, '__test__'
- If there are more please tell me!

I looked around the source-code and think I see the location where we can
do this.  It's would be a piece of cake and the advantages far outway the
disadvantages.  If I get some support I'd love to incorporate the fix.

Justin Shaw
thomas.j.shaw@aero.org

Follow-Ups:

Date: 2001-Feb-10 20:18
By: justinshaw

Comment:
jhylton,
Consider the synergistic effect of the niceties python offers.  Automatic
compilation, integrated documentation, and standardized coding style to
name a few.  These are all things that can be completed externally in a
trivial manner, yet their combined effect is the reason we love python
(aside from incredibly simple and powerful object orientation).

I certainly have much to learn about python and unit testing.  Maybe I have
completely missed the boat with unit testing.  For team projects it clearly
makes sense to set up a unit-testing machine with test running software. 
But in my particular case, and I suspect many others', I maintain many self
testing libraries with application scripts, not full blown applications.  I
work alone without CVS support and am constantly updating and modifying the
libraries.  I enjoy the confidence unit testing gives me to make these
mods.  I don't expend any brain cycles on whether unit testing is current
because they always run on every import*.  I am an ardent supporter of
automation where it makes sense and I'm sure you can come up with many
instances where it does not make sense.  In those cases you are by no means
forced to automatically execute unit tests.

If your mechanism meets the following criteria I take back all I have said
and will eagerly download it:
1. The standard // Remember documentation before javadoc?
2. Portable
3. Tests run only when the code is updated
4. One time implimentation

(I'd settle for 2, 3, and 4.)

Thank you for your timely response, I hope you'll reconsider
Justin Shaw
Thomas.J.Shaw@aero.org
 
* Overkill I realize hence the feature request
-------------------------------------------------------

Date: 2001-Feb-09 15:35
By: jhylton

Comment:
I don't agree that unit tests should run automatically.  Nor do I think
adding magic to the language to support unit tests is necessary when it is
trivial to add some external mechanism.

-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=131480&group_id=5470