Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对json字符串python运行测试_Python_Json - Fatal编程技术网

对json字符串python运行测试

对json字符串python运行测试,python,json,Python,Json,我目前正在尝试对JSON字符串运行一些测试,但是我遇到了一些困难 这是我到目前为止所拥有的 class PinpyTests(jsonstr, campaign): data = json.loads(jsonstr) test = False def dwellTest(self): if self.data.get('dwellTime', None) is not None: if self.data.get('dwell

我目前正在尝试对JSON字符串运行一些测试,但是我遇到了一些困难

这是我到目前为止所拥有的

class PinpyTests(jsonstr, campaign):

    data = json.loads(jsonstr)
    test = False

    def dwellTest(self):
        if self.data.get('dwellTime', None) is not None:
            if self.data.get('dwellTime') >= self.campaign.needed_dwellTime:
                # Result matches, dwell time test passed.
                self.test = True

    def proximityTest(self):
        if self.data.get('proximity', None) is not None:
            if self.data.get('proximity') == self.campaign.needed_proximity:
                # Result matches, proximity passed.
                self.test = True 
基本上,我需要运行测试,只要它们存在于json字符串中。因此,如果字符串中存在接近性,它将运行接近性测试等(可能会有更多测试,而不仅仅是这两个)


当两个测试都存在并且都需要返回true时,问题似乎就出现了。如果它们都返回true,那么测试已经通过,类可以返回true,但是,如果驻留失败,并且接近通过,我仍然需要它失败,因为不是所有测试都通过。(在接近的情况下通过)。对于如何继续,我有点困惑。

首先,您的类定义不正确。您可能需要的是一个uu init uu函数。为了获得您想要的结果,我建议添加一个testAll方法,检查json中的每个测试,然后运行该测试

class PinpyTests(Object):
    test = False

    def __init__(self, jsonstr, campaign):
        self.data = json.loads(jsonstr)
        self.campaign = campaign

    def testAll(self):
        passed = True
        if self.data.get('dwellTime') is not None:
            passed = passed and self.dwellTest()
        if self.data.get('proximity') is not None:
            passed = passed and self.proximityTest()
        return passed

    def dwellTest(self):
        if self.data.get('dwellTime') >= self.campaign.needed_dwellTime:
            # Result matches, dwell time test passed.
            return True
        return False

    def proximityTest(self):
        if self.data.get('proximity') == self.campaign.needed_proximity:
            # Result matches, proximity passed.
            return True
        return False

我认为你的类定义可能是错误的。您是否希望从jsonstr和campaign继承PinpyTests类?现在就是这样设置的。我怀疑您可能希望jsonstr和campaign成为uuu init_uuuuu函数的参数。很好,我写这篇文章时半睡半醒。不使用
unittest
框架的原因是什么?您可以根据字符串内容形成一个套件,然后运行套件。虽然我很感激您纠正了这个令人昏昏欲睡的错误,但这并不能解决原始问题。评论已经足够了,这并没有回答OPs问题。