3

I have a file and want to find elements within it.

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

if __name__ == '__main__':
    unittest.main()
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • The line self.assertTrue... is a https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions if you do not feel comfortable with that, you'd better expand it to a classical for loop notation, so that you can add your 'young' test. – nicolallias Jul 03 '18 at 07:25

3 Answers3

1

Add another line to test_description and use the in keyword:

self.assertTrue(any('young' in c['Description'] for c in charities))

see:

print(('young' in 'abc', 'young' in 'abc young'))
jmunsch
  • 22,771
  • 11
  • 93
  • 114
1

First, you need to fetch a list of all Charities containing 'Make a Wish' in the Description.

haveWish = [c for c in charities if c['Description'] == 'Make a Wish']

Then you can check if all of them have 'young' in Slogan

self.assertTrue(all('young' in c['Slogan'] for c in haveWish))
taras
  • 6,566
  • 10
  • 39
  • 50
1

try this code

self.assertTrue(any((c['Description'] == 'Make a Wish') and ('young' in c['Slogan']) for c in charities))                

using "in" for checking substring check out this link