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