如何测试网站在Python单元测试中具有200个响应代码?
因此,在本单元测试文件中,我尝试测试“ Wikivoyage”的响应代码为200,当我测试它是否正常时,那时我尝试创建一个错误的测试时,开始说404的响应代码现在是通过测试,是否有任何方法仅测试200
############### python代码
import requests
import json
used_country_list = [
"Nigeria",
# "South_West_Nigeria",
# "Lagos_State",
# "Lagos_City",
# "Amuwo_odofin",
# # California Detail
# "California",
# "Southern_California",
"Los_Angeles",
# "Los_Angeles/Eastside",
# "Los_Angeles/Downtown",
# "Bay_Area",
# German etc.
"Germany",
"Frankfurt",
]
def wikivoyage_extract(used_country_list):
print(f"[Extractor - WikiVoyage] pre loop")
current_dict = {}
for index, country_name in enumerate(used_country_list):
url = f"http://en.wikivoyage.org/w/api.php?action=query&prop=extracts&titles={country_name}&format=json"
print(f"[Extractor - WikiVoyage] url is {url}")
response = requests.get(url)
content = json.loads(response.content)
print(f"[Extractor - WikiVoyage] response is {response.status_code}")
pages = content['query']['pages']
print(f"{index} : {url}")
for count, key in enumerate(pages):
if count>0:
print(country_name, count)
extract = pages[key]['extract']
current_dict[country_name] = extract
return current_dict
wiki_dict = wikivoyage_extract(used_country_list)
################## ######### unitest代码##############
import unittest
from unit_test import wikivoyage_extract
class ResponseCodeTestCase(unittest.TestCase):
def test_is_wikivoyage_200(self):
self.assertEqual(wikivoyage_extract) == 200
def test_is_wikivoyage_404(self):
self.assertFalse(wikivoyage_extract) == 404
if __name__ == "__main__":
unittest.main()
So in this unit test file I have tried testing that "WikiVoyage" has a response code of 200 and when I tested that it was fine, it was then when I tried creating a false test it started saying that the response code of 404 was now passing the test, is there any way of testing just the response code of 200
############## python code #############
import requests
import json
used_country_list = [
"Nigeria",
# "South_West_Nigeria",
# "Lagos_State",
# "Lagos_City",
# "Amuwo_odofin",
# # California Detail
# "California",
# "Southern_California",
"Los_Angeles",
# "Los_Angeles/Eastside",
# "Los_Angeles/Downtown",
# "Bay_Area",
# German etc.
"Germany",
"Frankfurt",
]
def wikivoyage_extract(used_country_list):
print(f"[Extractor - WikiVoyage] pre loop")
current_dict = {}
for index, country_name in enumerate(used_country_list):
url = f"http://en.wikivoyage.org/w/api.php?action=query&prop=extracts&titles={country_name}&format=json"
print(f"[Extractor - WikiVoyage] url is {url}")
response = requests.get(url)
content = json.loads(response.content)
print(f"[Extractor - WikiVoyage] response is {response.status_code}")
pages = content['query']['pages']
print(f"{index} : {url}")
for count, key in enumerate(pages):
if count>0:
print(country_name, count)
extract = pages[key]['extract']
current_dict[country_name] = extract
return current_dict
wiki_dict = wikivoyage_extract(used_country_list)
############## unitest code ##############
import unittest
from unit_test import wikivoyage_extract
class ResponseCodeTestCase(unittest.TestCase):
def test_is_wikivoyage_200(self):
self.assertEqual(wikivoyage_extract) == 200
def test_is_wikivoyage_404(self):
self.assertFalse(wikivoyage_extract) == 404
if __name__ == "__main__":
unittest.main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们要求的就是这样:
现在,实际上,您不会使用该确切的行,因为如果状态代码不是200,那将使应用程序崩溃,但是您需要知道您应该做什么。你不应该返回吗?您应该记录问题吗?
What they're asking for is SOMETHING like this:
Now, in reality you wouldn't use that exact line, because that will crash the app if the status code is not 200, but you'll need to know what you SHOULD do. Should you return None? Should you log the problem?