0

I have a Python file, let's call it script1.py. I am trying to write a unit test (using unittest) called script1_test.py. script1 is meant to be called from command line and take in a number of arguments. When script 1 is run, it starts off with:

if __name__ == "__main__" and len(sys.argv) == 6:
    func1()
else
   print "Wrong number of arguments"
   sys.exit(1)

I'm just trying to execute and test a function (here called func1) within script1 independent of the main body of the code. But when I do so, I keep hitting the sys.exit from main during the import phase. How can I run the test without hitting this error?

1 Answer 1

1

When you do the import of your script, __name__ is not equal to main so you're calling the else block. Instead you should nest your if blocks:

if __name__ == "__main__":
    if len(sys.argv) == 6:
       func1()
    else:
       print "Wrong number of arguments"
       sys.exit(1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.