[Tutor] Query regarding Unittest module behaviour

Luke Paireepinart rabidpoobear at gmail.com
Tue Jul 4 12:36:55 CEST 2006


[snip]
> Script abc.py imports xyz.py.
> Now when I execute abc.py from commandlline all unittestcases of 
> xyz.py are also executed.
> Why is this happening and what can be the solution to this.
anything that's in the global scope  of an imported module gets executed.
For example...

------ a.py
print "hello"
------------

 >>> import a
This will cause the "hello" to be printed from 'a.py' because it's in 
the global scope.
so what you want to do is something like this:

-----a.py

def main():
    print "hello"

if __name__ == "__main__":
    main()
-------

Now if you do
 >>> import a
nothing will be printed.

But, if you do "python a.py" on command line, you'll see "hello" be printed.
I think this is the problem you're having.
HTH,
-Luke


More information about the Tutor mailing list