As mentioned by Ed I, assertIn
è probabilmente il più semplice rispondi a trovare una stringa in un'altra. Tuttavia, la questione afferma:
I want to make sure that my result
contains at least the json object (or string) that I specified as the second argument above,i.e., {"car" : ["toyota","honda"]}
Perciò vorrei utilizzare più asserzioni in modo che i messaggi vengono ricevuti utile in caso di fallimento - i test dovranno essere compreso e mantenuto in futuro, potenzialmente da qualcuno che non li scrivere in origine .Quindi supponendo che siamo all'interno di un django.test.TestCase
:
# Check that `car` is a key in `result`
self.assertIn('car', result)
# Compare the `car` to what's expected (assuming that order matters)
self.assertEqual(result['car'], ['toyota', 'honda'])
che dà utili messaggi come segue:
# If 'car' isn't in the result:
AssertionError: 'car' not found in {'context': ..., 'etc':... }
# If 'car' entry doesn't match:
AssertionError: Lists differ: ['toyota', 'honda'] != ['honda', 'volvo']
First differing element 0:
toyota
honda
- ['toyota', 'honda']
+ ['honda', 'volvo']
farebbe [https://docs.djangoproject.com/en/1.11/topics/testing/tools /#django.test.Response.json] aiuto? – coya