I have one python file wrote like this:
import sys
cfgfile = sys.argv[1]
cfg = ConfigObj(cfgfile)
db_dbname=cfg.get("DB_NAME")
class UserMappingsLoader:
def __init__(self):
self.debug = 0
self.cuStartTime = 0
self.fiId = 0
and I want to write a pyunit to do the testing for one method in this code. But when I run my testing code, it shows:
cfgfile = sys.argv[1]
list index out of range
Anyone know how to mock the sys.argv from testing file to this file?
You can do this using patch
from the mock module. Here is an example:
from mock import patch
def test_your_function():
fake_args = [None, "myfakearg"]
with patch('sys.argv', fake_args):
import yourmodule
# rest of your test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.