Woootiness
Woootiness

Reputation: 1952

What is the difference between "from mock import patch" and "from unittest.mock import patch"?

What is the difference between these imports?

from mock import patch

vs

from unittest.mock import patch

Are they the same?

Upvotes: 22

Views: 8404

Answers (2)

Klaus D.
Klaus D.

Reputation: 14404

The mock library has been integrated into the Python standard library from Python version 3.3 on as unittest.mock. They deliver the same functionality.

Nowadays the (external) mock library is a backport of the version in the standard library. If you are using a recent version of Python and don't have any special version requirements, the version from the standard library should be preferred.

Upvotes: 40

HamzaMushtaq
HamzaMushtaq

Reputation: 1850

Yes, both are the same but there is one major difference in them. It looks like the Mock version used in python mock is 1.0.0 which caused errors in my test cases due to dependency on the latest version.

https://github.com/python/cpython/blob/c1f1ddf30a595c2bfa3c06e54fb03fa212cd28b5/Lib/unittest/mock.py#L26

Upvotes: 1

Related Questions