Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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
Python 比较JSON字符串会得到相同代码的不同结果_Python_Json_Python 3.x_Unit Testing_Debugging - Fatal编程技术网

Python 比较JSON字符串会得到相同代码的不同结果

Python 比较JSON字符串会得到相同代码的不同结果,python,json,python-3.x,unit-testing,debugging,Python,Json,Python 3.x,Unit Testing,Debugging,因此,我有一个模块可以从中检索JSON。用户必须输入位置和API密钥才能接收相应位置的输出: (主模块) 我还有一个相同的测试文件: (测试仪模块) def测试\天气\响应(自身): 全局json_de 全局json_ny 自我评估(天气响应(“德里”) 输出文件: 从主要功能: 从测试功能: 测试2: 结果: 输出文件: 从主要功能: 从测试功能: 测试\u 3: 结果: 输出文件: 从主要功能: 从测试功能: 现在,我不知道为什么对于相同的代码、相同的url和相同的输出(请参阅文本文件

因此,我有一个模块可以从中检索JSON。用户必须输入位置和API密钥才能接收相应位置的输出:

(主模块)

我还有一个相同的测试文件:

(测试仪模块)

def测试\天气\响应(自身):
全局json_de
全局json_ny
自我评估(天气响应(“德里”)

输出文件:

从主要功能:

从测试功能:

测试2:

结果:

输出文件:

从主要功能:

从测试功能:

测试\u 3:

结果:

输出文件:

从主要功能:

从测试功能:



现在,我不知道为什么对于相同的代码、相同的url和相同的输出(请参阅文本文件),我只在第三次执行时出错。

所以问题是每个响应都附带了消息ID:


我跳过了这一行,问题就解决了。

您正在调用一个外部API;天气预报会随着时间的推移而变化。但我没有在脚本中硬编码任何内容。我正在为两个文件中的每次运行调用一个外部API。单元测试决不能依赖外部代码,尤其是您无法控制的外部服务产品随着时间的推移,你的工作不是测试他们的API。请解释下一票(是谁做的)。很难理解我做错了什么。是的,你多次调用一个外部API,然后当它们突然没有为多次调用返回完全相同的数据时,你的测试失败。这不是一个好的测试,它是脆弱的,无论如何都是错误的。
def weather_response(location, API_key):
    location=format(location) #Formats the location name if proper form
    url='http://api.openweathermap.org/data/2.5/forecast?q='+location+'&APPID='+API_key
    json=urllib.request.urlopen(url)
    json=json.read()
    json=json.decode()
    urllib.request.urlretrieve(url,"main3.txt")

    return json
def test_weather_response(self):
    global json_de
    global json_ny
    self.assertEqual(weather_response("Delhi","<APPID>"),json_de)
    self.assertNotEqual(weather_response("Mumbai","<APPID>"),json_de)
    self.assertEqual(weather_response("delhi","<APPID>"),json_de)
    self.assertEqual(weather_response("  dElHi    ","<APPID>"),json_de)
    self.assertNotEqual(weather_response("Pizza","<APPID>"),json_de)
url='http://api.openweathermap.org/data/2.5/forecast?q=Delhi&APPID=<APPID>'
json_de=urllib.request.urlopen(url)
json_de=json_de.read()
json_de=json_de.decode()

url2='http://api.openweathermap.org/data/2.5/forecast?q=NewYork&APPID=<APPID>'
json_ny=urllib.request.urlopen(url2)
json_ny=json_ny.read()
json_ny=json_ny.decode()

urllib.request.urlretrieve(url,"tested3.txt")