Python Mocking with unitest.mock 2: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Python Mocking with unitest.mock =Mocking a Method=")
 
Line 4: Line 4:


=Mocking a Method=
=Mocking a Method=
The general approach is to replace at runtime the real method instance associated with the class instance to be tested with a <code>Mock</code> instance, configured to simulate various behaviors of the real method.
Assuming there is a class <code>SomeClass</code> that has a method <code>some_method()</code> whose behavior we want to simulate during testing
==Simulating a Particular Return Value Irrespective of the Arguments it was Called With==

Revision as of 22:53, 1 June 2023

Internal

Mocking a Method

The general approach is to replace at runtime the real method instance associated with the class instance to be tested with a Mock instance, configured to simulate various behaviors of the real method.

Assuming there is a class SomeClass that has a method some_method() whose behavior we want to simulate during testing


Simulating a Particular Return Value Irrespective of the Arguments it was Called With