Is there a better way of accessing functions in a module?

A

Ant

I have the following code which works fine for running some tests
defined within a module:

def a_test():
print "Test A"

def b_test():
print "Test B"

if __name__ == "__main__":
tests = ["%s()" % x for x in dir() if x.endswith("test")]

for test in tests:
eval(test)

But this feels like a hack... Is there a cleaner way for accessing the
functions of the current module similar to the __dict__ attribute of
classes? i.e. a way to access the local symbol table?
 
A

Ant

Ant wrote:
....
But this feels like a hack... Is there a cleaner way for accessing the
functions of the current module similar to the __dict__ attribute of
classes? i.e. a way to access the local symbol table?

Sorry - posted too soon. Found the globals() built-in...
 
M

Mike Kent

Yes, you can go that route. But since it appears that what you are
doing is unit testing related, and you are interested in aranging for
all of your unit test cases to be run automatically, I'd suggest using
the unittest module.
 
K

Kent Johnson

Ant said:
Ant wrote:
...

Sorry - posted too soon. Found the globals() built-in...

You can also
import __main__
tests = [x for x in dir(__main__) if x.endswith("test")]

for test in tests:
getattr(__main__, test)()

but I second the suggestion of looking in to unittest or one of the
other test frameworks.

Kent
 
B

Ben Finney

Ant said:
def a_test():
print "Test A"

def b_test():
print "Test B"

Incidentally, the convention is to name test functions as 'test_foo'
not 'foo_test'; this will make your module more easily compatible with
existing testing tools.
if __name__ == "__main__":
tests = ["%s()" % x for x in dir() if x.endswith("test")]

for test in tests:
eval(test)

No need for eval. (You already found globals(), so I'll use that.)

if __name__ == "__main__":
test_funcs = [x for name, x in globals()
if name.startswith("test") and hasattr(x, "__call__")
]

for test in test_funcs:
test()

I'll concur with other posters on this thread and encourage you to
consider using the standard 'unittest' module, and recommend 'nose'
for test discovery and execution:

<URL:http://somethingaboutorange.com/mrl/projects/nose/>
 
B

bruno at modulix

Ben Finney wrote:
(snip)
if __name__ == "__main__":
test_funcs = [x for name, x in globals()
if name.startswith("test") and hasattr(x, "__call__")
]

Any reason not to use callable(x) here ? (instead of hasattr(x, "__call__"))
 
B

Ben Finney

bruno at modulix said:
Ben Finney wrote:
(snip)
if __name__ == "__main__":
test_funcs = [x for name, x in globals()
if name.startswith("test") and hasattr(x, "__call__")
]

Any reason not to use callable(x) here ? (instead of hasattr(x, "__call__"))

Other than my ignorance until now of 'callable', no :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top