I have a method that takes argparse as a parameter.
def some_method (self, options):
if options.something == True:
#do this
Is there a way to call this method directly without making argparse?
at the moment, I have to make argparse before I call it.
parser = argparse.ArgumentParser(description='something')
parser.add_argument('-s', '--something', dest='something')
options = parser.parse_args()
options.something = True
x.some_method(options)
If you can modify the method, you can do this (though the layers of ifs aren't beautiful):
def some_method (self, options):
if isinstance(options, bool):
if options:
#do this
# you don't need to compare True to True, since it should be a boolean
if options.something:
#do this
Alternatively, you can do this:
class options:
something = true
x.some_method(options)
I'm sure there are other ways, but those are the first two solutions I can think of.
You can use the mock
library, with which you can write tests like this:
from mock import Mock
options = Mock()
options.something = True
# Add initialization of other attributes used in your test case here
x.some_method(options)
# make assertions
We are actually doing duck typing here, we don't have to prepare a parsed argument object, instead we use a mock object that behaves the same way in this situation.
What's more, the behavior of this mock object can be easily modified to test different cases.
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.