User Tools

Site Tools


python:mocks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:mocks [2025/05/12 18:00] – created carlpython:mocks [2025/05/12 18:03] (current) carl
Line 7: Line 7:
 Don't use Python mocks.   They don't work how you want.  Don't use Python mocks.   They don't work how you want. 
  
-  * Mocks only kind of work.   Code like `mock.patch()` Do not work at the module layer.  If your code imports the patched symbol before the patch comes in, any code that already references gets the unpatched version.  This means you have to chase down any existing references for it to work.  That in turn means Tests will work in isolation but not when part of a suite.  +  * Mocks only kind of work.   Code like `mock.patch()` Do not work at the module layer.  If your code imports the patched symbol before the patch comes in, any code that already references gets the unpatched version.  This means you have to chase down any existing references for it to work.  That in turn means Tests will work in isolation but not when part of a suite.  [[https://stackoverflow.com/questions/63116720/python-patching-with-unittest-mock-function-after-already-imported-from-module|Example1]].  [[https://stackoverflow.com/questions/20242862/why-python-mock-patch-doesnt-work|Example 2]].  
 +  * Mocks change behavior in very odd ways between releases.  Mocking functions in ''__init__.py'' files changed, [[https://stackoverflow.com/questions/79177223/specify-difference-between-module-and-function-in-a-unittest-mock-patch-attr|Example 3]]. [[https://github.com/python/cpython/issues/117860|Example 4]]
  
  
python/mocks.txt · Last modified: by carl