测试JSON阅读器课程
我写了一堂课来阅读JSON文件,以及一种从中获取某个参数的方法。该课程看起来像:
import argparse
import os
import json
class JsonReader:
def __init__(self):
# define parser and its arguments
parser = argparse.ArgumentParser(description='Json reader')
parser.add_argument('-c', '--config', help='Pass the configuration file in json format', required=True)
args = vars(parser.parse_args())
# verify the argument is ok (json file)
if args['config'][-4:] != "json":
raise Exception(f'Please, choose a json file.')
if not os.path.exists(args['config']):
raise Exception(f'The file doesn\'t exists, choose a correct file.')
with open(args['config']) as json_file:
self.data = json.load(json_file)
def get_setting(self, value: str):
try:
return self.data[value]
except:
raise Exception('"{}" is missing in the configuration file.'.format(value))
课程很好,但是当我尝试以Unitest进行测试时,存在一个问题。测试脚本看起来像:
import unittest
import os
import sys
from MyFilePath import JsonReader
class TestJsonReader(unittest.TestCase):
json_reader = JsonReader()
if __name__ == '__main__':
unittest.main()
来调用该类别的脚本
python -m unittest pythonfilename --config "my/path/file.json"
如果我尝试通过使用以下错误
usage: python.exe -m unittest [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [-k TESTNAMEPATTERNS] [tests ...]
:如果我不指定- config>
参数,我会得到此输出:
usage: python.exe -m unittest [-h] -c CONFIG
python.exe -m unittest: error: the following arguments are required: -c/--config
I wrote a class to read a JSON file, and a method to get a certain parameter from it. The class looks like:
import argparse
import os
import json
class JsonReader:
def __init__(self):
# define parser and its arguments
parser = argparse.ArgumentParser(description='Json reader')
parser.add_argument('-c', '--config', help='Pass the configuration file in json format', required=True)
args = vars(parser.parse_args())
# verify the argument is ok (json file)
if args['config'][-4:] != "json":
raise Exception(f'Please, choose a json file.')
if not os.path.exists(args['config']):
raise Exception(f'The file doesn\'t exists, choose a correct file.')
with open(args['config']) as json_file:
self.data = json.load(json_file)
def get_setting(self, value: str):
try:
return self.data[value]
except:
raise Exception('"{}" is missing in the configuration file.'.format(value))
The class works nicely, but there is an issue when I try to test it with unittest. The test script looks like:
import unittest
import os
import sys
from MyFilePath import JsonReader
class TestJsonReader(unittest.TestCase):
json_reader = JsonReader()
if __name__ == '__main__':
unittest.main()
If I try to call a script with that class by using:
python -m unittest pythonfilename --config "my/path/file.json"
I got the following error:
usage: python.exe -m unittest [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [-k TESTNAMEPATTERNS] [tests ...]
If I don't specify the --config
argument, I get this output:
usage: python.exe -m unittest [-h] -c CONFIG
python.exe -m unittest: error: the following arguments are required: -c/--config
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论