I'm having a python module eg: myexample.py with the below contents.
import os, sys
class nw_clss():
def new_func():
/*some statements*/
def main():
main(some arguments)
Now am writing a unittest framework where I would need to call some individual functions in the myexample.py. However, since there is a main method inside the myexample.py, whenever am trying to call an individual function, the framework is actually calling the main method and executing all methods available in myexample.py
Is there any possible to override this and just call the individual methods.
Thanks, Arjun
this is the purpose of
if __name__ == '__main__':
main()
which you might have seen from time to time.
when you run a module directly, its __name__
variable will be '__main__'
, but it will be different if imported by another module, so code in a block like this will only execute when that module is run directly.
EDIT: to clarify, what you want to have is
import os, sys
class nw_clss():
def new_func():
/*some statements*/
def main():
pass
if __name__ == '__main_':
main(some arguments)
EDIT 2: from the comments
in your unittest module if you only want to test 1 part of the module (eg the class) you can import only that:
from myexample import nw_clss
which won't run the whole module. It's less good that writing code with good practice, but if for some reason whoever wrote that original code won't let you then I guess that is what you have to do.