python / cpython

The Python programming language
https://www.python.org/
Other
59.79k stars 28.96k forks source link

__test__() should auto-exec at compile time #33877

Closed 3c2c9fbe-9877-4100-8246-6fab83fa8e5e closed 23 years ago

3c2c9fbe-9877-4100-8246-6fab83fa8e5e commented 23 years ago
BPO 231480

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: ```python assignee = None closed_at = created_at = labels = ['type-feature'] title = '__test__() should auto-exec at compile time' updated_at = user = 'https://bugs.python.org/justinshaw' ``` bugs.python.org fields: ```python activity = actor = 'justinshaw' assignee = 'jhylton' closed = True closed_date = None closer = None components = ['None'] creation = creator = 'justinshaw' dependencies = [] files = [] hgrepos = [] issue_num = 231480 keywords = [] message_count = 3.0 messages = ['3273', '3274', '3275'] nosy_count = 2.0 nosy_names = ['jhylton', 'justinshaw'] pr_nums = [] priority = 'normal' resolution = 'wont fix' stage = None status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue231480' versions = [] ```

3c2c9fbe-9877-4100-8246-6fab83fa8e5e commented 23 years ago

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:

Disadvantages:

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

03bde425-37ce-4291-88bd-d6cecc46a30e commented 23 years ago

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.

3c2c9fbe-9877-4100-8246-6fab83fa8e5e commented 23 years ago

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