lundi 27 juin 2016

How to test Generators in Python 3. It gives error all the time

I have snippet of the code below. Which I cannot change but I need to test. What it does is it connects to the server and gets data that I need. In particular I need to insert falsely input for variable data1 on line 12 to test it. But I can't achieve it so far.

1. url = "http://localhost:8000"
2. data = urllib.request.urlopen(url)
3.
4. def get_data():
5.     yield data.read()
6.
7. #generator
8. def get_objects(in_stream):
9.     json_object = ""
10.    buffer = b''
11.    for data1 in in_stream:
12.        data = buffer + data1
...

22.for json_dict in get_objects(get_data()):
23.   print(repr(json_dict))

get_object(in_stream) must be some sort of iterable, right? So, I am trying to pass a string there:

def test_falsely(self):
    self.assertEqual(solution.get_objects("bla bla"), "blabla")

But I am getting an error:

======================================================================
FAIL: test_falsely (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 21, in test_falsely
    self.assertEqual(solution.get_objects("bla bla"), "blabla")
AssertionError: <generator object get_objects at 0x103433308> != 'blabla'


What do I do wrong? Does anybody have idea how to test it? Thank you.

Aucun commentaire:

Enregistrer un commentaire