用于读取json文件的python unittest

时间:2019-02-26 02:02:06

标签: python json unit-testing

我的代码如下:

class ReportVersion(Task):

    BUILDS_INFO = os.path.join('/', 'home', '.cache', 'version.json')

    def __init__(self, executor):
        self.executor = executor
        self.build_no = self.executor.cmd_args['build_no']

    @provides_nothing
    def _run(self, test_result, backup_build):
        if test_result:
            with open(self.BUILDS_INFO, "w") as fl:
                 json.dump({"BUILDS":[self.build_no, backup_build]}, fl,  indent=2)

执行完此类后,如果test_result失败,则不会生成文件;如果test_result通过,则文件内容如下所示:

{

 "BUILDS": [

    "3333",
    "5555"

  ]

}

现在,我想编写一个测试用例来测试此类以检查文件内容,并且当测试结果失败时,不应生成该文件。我该怎么办?

感谢您的帮助!

0 个答案:

没有答案