How do I test that a website has a response code of 200 in python unit testing?

795 Views Asked by At

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()
1

There are 1 best solutions below

0
Tim Roberts On

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