如何测试网站在Python单元测试中具有200个响应代码?

发布于 2025-02-08 02:33:25 字数 1843 浏览 0 评论 0原文

因此,在本单元测试文件中,我尝试测试“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2025-02-15 02:33:25

他们要求的就是这样:

import requests
import json

used_country_list = [
    "Nigeria",
    "Los_Angeles",
    "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}")
        assert response.status_code == 200

        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)

现在,实际上,您不会使用该确切的行,因为如果状态代码不是200,那将使应用程序崩溃,但是您需要知道您应该做什么。你不应该返回吗?您应该记录问题吗?

        ...
        if response.status_code != 200:
            print( "*** Bad status code received" )
            return None

What they're asking for is SOMETHING like this:

import requests
import json

used_country_list = [
    "Nigeria",
    "Los_Angeles",
    "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}")
        assert response.status_code == 200

        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)

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?

        ...
        if response.status_code != 200:
            print( "*** Bad status code received" )
            return None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文