mardi 3 juillet 2018

JSON Testing in Python

I have a JSON file and want to test elements within it. I want to test whether the Charities element with Description = "Make a Wish" has a Slogan that contains "young". Below is the code that checks for the Description which passes but don't know how to check if it contains the "young" text

import unittest
import json
import requests


class Test(unittest.TestCase):


    def test_description(self):
        api_url = 'https://api.myjson.com/bins/mtthu'
        r = requests.get(api_url)
        j = r.json()
        charities = j.get('Charities', [])
        self.assertTrue(any(c['Description'] == 'Make a Wish' for c in charities))

if __name__ == '__main__':
    unittest.main()

Aucun commentaire:

Enregistrer un commentaire