How do you get to the value inside of a python function that returns nothing for unit testing?

Muhammad Lukman Low

I am trying to write a unit test for an existing function:

def _fetch_law(self, task, storage):
    number, link = task
    if not self.silent:
        print 'Requesting page %s' % number
    r = requests.get(link, headers=self.headers)

    if r.status_code != 200:
        sys.exit('CONNECTION ERROR!!! HTTP ERROR %d' % r.status_code)
    vs = LawPages(r.text)
    storage.extend(vs.extract())

For my unit testing, how do I use mocking or other means to get to the final value that is passed to storage ?

mgilson

I generally use the mock library (unittest.mock in python3.x). It would look something like:

mock_storage = mock.Mock()
obj._fetch_law(some_task, mock_storage)
mock_storage.extend.assert_called_with(expected_value)

Of course, in this example, you might want to mock out requests as well... mock.patch is great for that:

with mock.patch.object(requests, 'get') as mock_get:
    mock_get.return_value = ' ... '
    mock_storage = mock.Mock()
    obj._fetch_law(some_task, mock_storage)
    mock_storage.extend.assert_called_with(expected_value)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do get an HTML form submit (and Python function return) to do nothing using Flask

How do you structure imports for testing inside a module of your project?

How do you Unit Test Python DataFrames

How to create function that returns nothing

Trouble finding the last input value of a table with jQuery, my function only returns jibberish. How do you get the last row input value?

Unit-testing Python: Mocking function calls inside function

How do you mock a return value from PyMySQL for testing in Python?

How do you print a function that returns a request in Python?

How do you know which components to import when unit testing?

How do you unit test a Vue.js functional component with a render function that returns any array of elements?

How to get value of nested props function for unit testing

How do you iterate a function in gdb with various inputs until you get a successful return value

How do you get the index of a JSON value in Python?

How do you get the value from a JavaScript function in Flutter WebView?

How do you know if loop or double loop is used in a function when unit testing?

How do you connect to database inside a function

How to do unit testing

how do you mock an xml for unit testing?

how to get a value inside a function

How do you get the value inside an XML document?

How do you await a Task when unit testing?

How do you get the "this" of a function?

Unit testing a function that returns a pointer (C++)

How do I get fake path for HttpContext.Current.Server.MapPath which is assigned to protected object inside method unit testing?

python function returns nothing

How do you get the X value in KMeans clustering in python?

How do you get the value of an async operation inside a non-async function?

How do I call a function inside the function that you are calling to [PYTHON]

How do you check if your function returns nothing?

TOP Ranking

HotTag

Archive