2

I am trying to implement unittest in python. I have a list which I need to verify.

Therefore I do

self.assertEqual(self.yt.get_videos(), self.videos)

Error:

AssertionError: Lists differ: [<Video: MPEG-4 Visual (.3gp) ... != ['<Video: MPEG-4 Visual (.3gp)...

First differing element 0:
<Video: MPEG-4 Visual (.3gp) - 144p - Simple>
<Video: MPEG-4 Visual (.3gp) - 144p - Simple> 

Output of both.

>>> pprint(yt.get_videos())
[<Video: MPEG-4 Visual (.3gp) - 144p - Simple>,
 <Video: MPEG-4 Visual (.3gp) - 240p - Simple>,
 <Video: Sorenson H.263 (.flv) - 240p - N/A>,
 <Video: H.264 (.mp4) - 360p - Baseline>,
 <Video: H.264 (.mp4) - 720p - High>,
 <Video: VP8 (.webm) - 360p - N/A>]

Below list I have formed by my own.

>>> pprint(videos)
['<Video: MPEG-4 Visual (.3gp) - 144p - Simple>',
 '<Video: MPEG-4 Visual (.3gp) - 240p - Simple>',
 '<Video: Sorenson H.263 (.flv) - 240p - N/A>',
 '<Video: H.264 (.mp4) - 360p - Baseline>',
 '<Video: H.264 (.mp4) - 720p - High>',
 '<Video: VP8 (.webm) - 360p - N/A>']

How can I define my list to have elements without quotes.

Tan
  • 51
  • 4

3 Answers3

0

the first is a list of objects, the second a list of strings.

Convert one of the two to make them match.

Probably the easiest is to convert them in the test:

self.assertEqual([str(el) for el in self.yt.get_videos()], self.videos)

or, as @Peter Wood points out

self.assertEqual(map(str, self.yt.get_videos()), self.videos)

Of course you need to define the __str__/__repr__ methods in your class for str to work, and their output must match the format of the strings.

Please see this SO QA for more info about str and repr.

Community
  • 1
  • 1
Pynchia
  • 10,996
  • 5
  • 34
  • 43
0

What are you trying to check in your test? That the videos are identical (may be unlikely depending on how they are generating - even videos that look identical may differ, just one pixel being slightly of in color in one frame makes a difference) or that only their string representations are identical?

For the first case you need to have a list of video objects, not just their string representation.

For the second case which is much simpler you don't want to convert the self.videos to an "array without quotes". You want to convert the first, like:

self.assertEqual([str(v) for v in self.yt.get_videos()], self.videos)

Note that this will only check that the string representation matches. For example the first will only be a check that it's a MPEG-4 gp3 video, but not that it's content actually matches.

skyking
  • 13,817
  • 1
  • 35
  • 57
0

Try to compare:

self.assertEqual([str(el) for el in self.yt.get_videos()], self.videos)

And check repr method here

xiº
  • 4,605
  • 3
  • 28
  • 39