Can I fake/mock the type of my mock objects in python unittests

2024/11/18 19:57:08

In my python code I check the type of one of the parameters to make sure it is of the type I expect. For instance:

def myfunction(dbConnection):if (type(dbConnection)<>bpgsql.Connection):r['error'] += ' invalid database connection'

I want to pass a mock connection for testing purposes. Is there a way to make the mock object pretend to be of the correct type?

Answer

With all due respect, It looks like you guys are not quite correct!

I can use duck typing as said, but there is a way to do what I intended to do in the first place:

from http://docs.python.org/dev/library/unittest.mock.html

Mock objects that use a class or an instance as a spec or spec_set are able to pass isintance tests:

>>>
>>> mock = Mock(spec=SomeClass)
>>> isinstance(mock, SomeClass)
True
>>> mock = Mock(spec_set=SomeClass())
>>> isinstance(mock, SomeClass)
True

so my example code would be like:

m = mock.MagicMock(spec=bpgsql.Connection)
isinstance(m, bpgsql.Connection) 

this returns True

All that said, I am not arguing for strict type checking in python, I say if you need to check it you can do it and it works with testing and mocking too.

https://en.xdnf.cn/q/26531.html

Related Q&A

Changing the background color of the axes planes of a 3D plot

On the basis of the scatterplot example of matplotlib, how can I change the gray background color of the 3 axes grid planes? I would like to set it to white, keeping the grid lines with the default gr…

Select checkbox using Selenium with Python

How can I select the checkbox using Selenium with Python? from selenium import webdriver from selenium.webdriver.common.keys import Keysbrowser = webdriver.Firefox() url = Any URL browser.get(url)brow…

linear programming in python?

I need to make a linear programming model. Here are the inequalities Im using (for example):6x + 4y <= 24 x + 2y <= 6 -x + y <= 1 y <= 2I need to find the area described by these inequaliti…

Beginner Python Practice? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

Pylint: Disable specific warnings for specific folder

We have a Python project laid out like this:project/ ├── .pylintrc ├── module1.py ├── module2.py └── tests/├── test_module1.py└── test_module2.pyOur unit and function tests reside in …

PyCharm: Configuring multi-hop remote Interpreters via SSH

To connect to the computer at my office I need to run ssh twice. First to connect to the host-1 and then from host-1 to host-2 and each one has different credentials. However the configuration menu in …

Return results from multiple models with Django REST Framework

I have three models — articles, authors and tweets. Im ultimately needing to use Django REST Framework to construct a feed that aggregates all the objects using the Article and Tweet models into one r…

Comparing XML in a unit test in Python

I have an object that can build itself from an XML string, and write itself out to an XML string. Id like to write a unit test to test round tripping through XML, but Im having trouble comparing the tw…

Passing a tuple as command line argument

My requirement is to pass a tuple as command line argument like --data (1,2,3,4)I tried to use the argparse module, but if I pass like this it is receiving as the string (1,2,3,4). I tried by giving ty…

Setting exit code in Python when an exception is raised

$ cat e.py raise Exception $ python e.py Traceback (most recent call last):File "e.py", line 1, in <module>raise Exception Exception $ echo $? 1I would like to change this exit code fr…