Skip to content
Advertisement

Python mocking table schema

I have a simple python function that needs tested that looks like this:

JavaScript

However, to test this, I don’t want to test with a real table – I would like to create a Mock schema within my test (and a mock client too).

My test currently looks like this:

JavaScript

However, when I print returned_schema, I get the following: <MagicMock name='Client.get_table().schema' id='4591155568'>.

Can anyone advise what I am doing wrong?

Advertisement

Answer

Well, you set the value for Table.schema but you have already mocked google.cloud.bigquery.Client. When you access any value on your mock_client you just get another magic mock – but you can define what these mocks should return.

So you need to set the expected return value on your mock_client directly.

Replace you code with this and it should work (although the test might seem a little pointless at that point)

JavaScript

Although if you want to test more properties I’d suggest the following approach:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement