Hello I am new to programming and and trying to make a test that checks whether any items in a list of items are present in another list (using unittest in Python 2.7).
For example if I have a list ["dog", "cat", "frog] and the result of the method I am testing is ["tiger", "lion", "kangaroo", "frog] I want the test to fail because it contains one of the items of the previous list ("frog"). I would also like the test to tell me what words did both lists have (i.e. what words caused the test to be a fail).
I have tried:
self.assertIn(["cat", "dog"], method("cat dog tiger"))
The result of the method is ["cat", "dog", "tiger"] yet the result of the test is a fail and says:
AssertionError: ['cat', 'dog'] not found in ['cat', 'dog', 'tiger']
I want this test to return ok because 'cat' and 'dog' are present in the second list. It seems assertIn doesn't do what I thought it would do (I thought it was to check if any of a are present in b).
And vice versa, assertNotIn passes when I want it to fail.
I've been searching for a while now but because I'm not sure what I'm looking for which makes it hard to find.
Thank you for reading, I hope that makes sense.
EDIT: I have gone with Chris's solution and it works as I want:
def myComp(list1, list2):
section = list(set(list1).intersection(list2))
To get the list of words that overlap (i.e. triggering the fail) in the error message, I added the code below from here How to change the message in a Python AssertionError?:
try:
assert(len(section)==0)
except AssertionError as e:
e.args += ('The following are present in the processed text',
section)
raise
The result is exactly what I want:
AssertionError: ('The following are pressent in the processed text', ['dog',
'cat'])
The assert section ensures that the code behaves as expected. Assertions replace us humans in checking that the software does what it should. They express requirements that the unit under test is expected to meet.
We can test if two sets are equal using the normal “==” operator. Notice – since sets are unordered, it doesn't matter that the set other_nums looks like its elements are in a different order. Set equality comparison just takes into account if the two sets share the exact same elements, regardless of order.
TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.
There are two ways you can use assertRaises: using keyword arguments. Just pass the exception, the callable function and the parameters of the callable function as keyword arguments that will elicit the exception. Make a function call that should raise the exception with a context.
You can either iterate through your list and assertIn
, or use set
s and you could do something like self.assertTrue(set(a).issuperset(set(b)))
.
self.assertTrue(any(animal in method("cat dog tiger") for animal in ("cat", "dog")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With