Python 使用 Doctest 的单元测试未按预期工作
Python Unit Testing using Doctest not working as expected
我创建了一个 python 模块,通过将特定位置作为输入来生成天气数据(纬度、经度、海拔和其他详细信息)。
根据标准对其进行了更新,"pycodestyle" 用于检查 PEP8 标准的包不会引发任何错误或警告。
我的代码如下:
def fetch_location_info(input_list, err_file):
# URL which gives us Latitude, Longitude values
LatLong_URL = (
'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='
)
# URL which gives us Elevation values
Elevation_URL = (
'https://maps.googleapis.com/maps/api/elevation/json?locations='
)
# Initializing Error Logs with relevant title for writing error records
err_line_header = "Logging Location Data Errors"
print(err_line_header, file=err_file)
# Insert a new line in the error file after the Error Header
print("\n", file=err_file)
# Fetch and Extract Location details from google maps
input_info = []
for location in input_list:
temp_info = {'Location': location}
latlong_response = requests.get(LatLong_URL + location).json()
if latlong_response.get('results'):
for latlong_results in latlong_response.get('results'):
latlong = (
latlong_results
.get('geometry', '0')
.get('location', '0')
)
temp_info['Latitude'] = latlong.get('lat', '0')
temp_info['Longitude'] = latlong.get('lng', '0')
elevation_response = requests.get(
Elevation_URL
+ str(temp_info['Latitude'])
+ ','
+ str(temp_info['Longitude'])
).json()
if elevation_response.get('results'):
for elevation_results in elevation_response.get('results'):
temp_info['Elevation'] = (
elevation_results.get('elevation', '0'))
input_info.append(temp_info)
break
else:
print("Elevation_URL is not fetching values for {}"
.format(location),
file=err_file
)
break
else:
print("LatLong_URL is not fetching values for {}"
.format(location),
file=err_file
)
print("\n", file=err_file)
return input_info
下一步,我正在尝试使用 doctest 进行单元测试。 我选择将测试用例保存在单独的文件中。所以我创建了以下 .txt 文件 并保存在与代码相同的目录中。
This is a doctest based regression suite for Test_Weather.py
Each '>>' line is run as if in a python shell, and counts as a test.
The next line, if not '>>' is the expected output of the previous line.
If anything doesn't match exactly (including trailing spaces), the test fails.
>>> from Test_Weather import fetch_location_info
>>> fetch_location_info(["Sydney,Australia"], open('data/error_log.txt', 'w'))
print(input_info)
如上所示,预期条件应 return 在被测函数中创建的列表/数据框/变量的内容。作为尝试,我只是尝试打印列表的内容,但我的单元测试输出抛出如下错误,因为预期值和得到的值不匹配:
PS C:\Users\JKC> python -m doctest testcases.txt
********************************************************************** File "testcases.txt", line 7, in testcases.txt Failed example:
fetch_location_info(["Sydney,Australia"], open('data/error_log.txt', 'w'))
预计:
print(input_info)
得到:
[{'Location': 'Sydney,Australia', 'Latitude': -33.8688197, 'Longitude': 151. 2092955, 'Elevation': 24.5399284362793}]
所以在这里你可以看到测试用例工作正常但由于我无法打印列表的内容,它没有通过测试用例。
我的问题是如何在单元测试用例的预期部分显示列表的内容?
如果我没记错的话,我需要在单元测试用例的预期部分中逐字提及输出值吗?
任何输入都会有所帮助
你需要让 doctest 看起来就像你 运行 在 Python REPL:
>>> from Test_Weather import fetch_location_info
>>> fetch_location_info(["Sydney,Australia"], open('data/error_log.txt', 'w'))
[{'Location': 'Sydney,Australia', 'Latitude': -33.8688197, 'Longitude': 151.2092955, 'Elevation': 24.5399284362793}]
我创建了一个 python 模块,通过将特定位置作为输入来生成天气数据(纬度、经度、海拔和其他详细信息)。
根据标准对其进行了更新,"pycodestyle" 用于检查 PEP8 标准的包不会引发任何错误或警告。
我的代码如下:
def fetch_location_info(input_list, err_file):
# URL which gives us Latitude, Longitude values
LatLong_URL = (
'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='
)
# URL which gives us Elevation values
Elevation_URL = (
'https://maps.googleapis.com/maps/api/elevation/json?locations='
)
# Initializing Error Logs with relevant title for writing error records
err_line_header = "Logging Location Data Errors"
print(err_line_header, file=err_file)
# Insert a new line in the error file after the Error Header
print("\n", file=err_file)
# Fetch and Extract Location details from google maps
input_info = []
for location in input_list:
temp_info = {'Location': location}
latlong_response = requests.get(LatLong_URL + location).json()
if latlong_response.get('results'):
for latlong_results in latlong_response.get('results'):
latlong = (
latlong_results
.get('geometry', '0')
.get('location', '0')
)
temp_info['Latitude'] = latlong.get('lat', '0')
temp_info['Longitude'] = latlong.get('lng', '0')
elevation_response = requests.get(
Elevation_URL
+ str(temp_info['Latitude'])
+ ','
+ str(temp_info['Longitude'])
).json()
if elevation_response.get('results'):
for elevation_results in elevation_response.get('results'):
temp_info['Elevation'] = (
elevation_results.get('elevation', '0'))
input_info.append(temp_info)
break
else:
print("Elevation_URL is not fetching values for {}"
.format(location),
file=err_file
)
break
else:
print("LatLong_URL is not fetching values for {}"
.format(location),
file=err_file
)
print("\n", file=err_file)
return input_info
下一步,我正在尝试使用 doctest 进行单元测试。 我选择将测试用例保存在单独的文件中。所以我创建了以下 .txt 文件 并保存在与代码相同的目录中。
This is a doctest based regression suite for Test_Weather.py
Each '>>' line is run as if in a python shell, and counts as a test.
The next line, if not '>>' is the expected output of the previous line.
If anything doesn't match exactly (including trailing spaces), the test fails.
>>> from Test_Weather import fetch_location_info
>>> fetch_location_info(["Sydney,Australia"], open('data/error_log.txt', 'w'))
print(input_info)
如上所示,预期条件应 return 在被测函数中创建的列表/数据框/变量的内容。作为尝试,我只是尝试打印列表的内容,但我的单元测试输出抛出如下错误,因为预期值和得到的值不匹配:
PS C:\Users\JKC> python -m doctest testcases.txt ********************************************************************** File "testcases.txt", line 7, in testcases.txt Failed example: fetch_location_info(["Sydney,Australia"], open('data/error_log.txt', 'w'))
预计:
print(input_info)
得到:
[{'Location': 'Sydney,Australia', 'Latitude': -33.8688197, 'Longitude': 151. 2092955, 'Elevation': 24.5399284362793}]
所以在这里你可以看到测试用例工作正常但由于我无法打印列表的内容,它没有通过测试用例。
我的问题是如何在单元测试用例的预期部分显示列表的内容?
如果我没记错的话,我需要在单元测试用例的预期部分中逐字提及输出值吗?
任何输入都会有所帮助
你需要让 doctest 看起来就像你 运行 在 Python REPL:
>>> from Test_Weather import fetch_location_info
>>> fetch_location_info(["Sydney,Australia"], open('data/error_log.txt', 'w'))
[{'Location': 'Sydney,Australia', 'Latitude': -33.8688197, 'Longitude': 151.2092955, 'Elevation': 24.5399284362793}]