Applications Google
Menu principal

Post a Comment On: Only Python

"Python wish: a new meaning for "import ... as ...""

6 Comments -

1 – 6 of 6
Blogger EY said...

What's wrong with, e.g.:

def run_tests():
    # do stuff

if __name__ == '__main__':
    run_tests()

12:24 AM

Blogger Ian Bicking said...

With py.test (and maybe with nose, I'm not sure) you can just put the test in a function named test_*, and then run py.test file.py.

1:43 AM

Anonymous Anonymous said...

Interesting, but what happens when you import that module with different names in different places?

4:40 AM

Blogger André Roberge said...

Jason:
You are quite right. I was totally sidetracked looking at some C code and its preprocessor directives, and was thinking of the possibility of having
if __name__ == ...
statements sprinkled throughout the file. But, of course, with the dynamic nature of Python, this can be emulated (and in a better way) by doing
import module
module.run_test()
where run_test() can set the various flags needed.

Ian: Excellent point; I should look at py.test (and other testing frameworks).

Peter: That is exactly what I wanted to be able to do (see above comment regarding pre-processor in C)... but is not needed.
====
Note to self: do not post at the end of a day staring at some non-Python code...

7:35 AM

Anonymous Anonymous said...

IMHO testing by using __name__ == "__main__" at the end of a module is bad practice. The tests will need to be run individually and probably manually, so they will hardly ever get run.

It is much better to have the tests in a separate file and written using a testing framework such as unittest or py.test. This allows you to easily run the tests for an individual module, class or method, or to run the entire suite of tests across all modules in a project. You can easily automate the tests to run on checkin or as part of an overnight build. This means that the tests will get run frequently and become an integral part of the development process instead of an occasional afterthought.

I recommend reading the book 'Test Driven Development by Example' by Kent Beck to see just how integrated the tests can be with the development.

3:48 AM

Anonymous Anonymous said...

While I know many say that having the unit tests in a separate file is the right approach, I've found that having a __main__ self-test is very handy because I develop in Emacs so I can hit "control-C control-C" and run the current module. This makes it the code/debug cycle very fast. Also, I can get the function definitions and uses with the same text searches (as when changing the API) and not need to scan two different files.

Once that's done I can move the self-tests elsewhere, which also means making the hard coded filenames (like to /home/somebody/whatever) more portable -- something I don't want to worry about when doing the initial development.

2:03 AM

Spammers: none shall pass.
You can use some HTML tags, such as <b>, <i>, <a>

Comments on this blog are restricted to team members.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot