测试JSON阅读器课程

发布于 2025-01-25 13:35:18 字数 1723 浏览 2 评论 0原文

我写了一堂课来阅读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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文