[Python] Python Unittest for method returning json string

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I ma new to writing python unit tests. I have a method in a class returning a Json response from an API. The JSON response contains attributes such as data, token, object name and status. The method hits API and returns response with different values each time, so I can't hard code a response and test it. How can I write a unit test for this type of method.

    One thing, I thought of is to check whether the response is not null. Is there any other type of checks I can do here.

    Each time it returns a different token, date(timestamp). The status will be same.

    def Json_get_status(self):
    get_url = "xxxx" #URL to hit API
    r = requests.get(get_url)
    self.get_json = json.loads(r.text)
    self.get_token=self.get_json["token"]
    self.get_date=self.get_json["date"]
    self.get_status=self.get_json["status"]
    return self.get_json

    Continue reading...

Compartilhe esta Página