Questions tagged [pymock]

PyMock is an EasyMock like python mocking library

PyMock is based on the java easymock package http://www.easymock.org. It uses a recording and replay model rather than using a specification language. Easymock lives up to it's name compared to other mocking packages. PyMock takes advantage of python's dynamic nature for futher improvements.

In addition to allowing you to mock out simple function calls PyMock allows you to mock out properties, generators, and raw functions. It can also temporarily override all of these entities within existing classes and objects. (For instance you can mock out os.listdir() for a single test.

PyMock does have several weakeness. You can't specify dynamic parameter value checks, nor can you verify ordering between calls.

Be warned of several things. PyMock may not work if you're doing deep black magic with metaclasses. You're likely to run into its machinery. If you check object types explicitly you're likely to it's machinery too. In these cases you'll need to fall back to more traditional means of testing.

7 questions
12
votes
1 answer

Is it possible to mock a C function using python?

I was wondering is it possible to mock a C function using the Python mock library and how? To make a unit test for a C function in Python I use the shared library, but I can't mock one function in order to pass the test. Tests work fine when there…
P Jelic
  • 121
  • 4
2
votes
1 answer

Check mock called with runtime object create

I have code for cassandra cluster like cluster = Cluster( config.CASS_CLUSTER, load_balancing_policy=policies.DCAwareRoundRobinPolicy( config.CASS_D_CENTER)) When I write UT for this and try to check Cluster call with my…
Nilesh
  • 20,521
  • 16
  • 92
  • 148
1
vote
2 answers

Python Mock: raise 3rd party exception for unit testing

Let's say i have a method is_validate, which internally calls validate method from library gateway.service import gateway.service from gateway.service.exception import ValidatorException def is_validate(): try: gateway.service.validate()…
Abhinav manthri
  • 151
  • 1
  • 14
1
vote
1 answer

Mocking postgressql using python & pytest

def fetch_holidays_in_date_range(src): query = "SELECT * from holiday_tab where id = src" db = dbconnect.connect() # defining the cursor for reading data cursor = db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) # query…
1
vote
0 answers

Mock a method call inside another method

I have a method that I would like to unit test - run. During initialization of my class, I pass an object of another class to it. The said object fetches data with it's method. class MyClass: def __init__(self, data_obj): self.data_obj =…
Piyush
  • 606
  • 4
  • 16
  • 38
1
vote
0 answers

Why is mock created by PyMock considered False for assert?

Let's consider this example: class First: def register(self, handler): pass class Second: def __init__(self, first): assert first self.__first = first And test injecting mock: class TestSecond(PyMockTestCase): …
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
1
vote
0 answers

Constructor injection and PyMock

How to correctly record mock for this example (and simplified) scenario? class First: def register(self, handler): pass class Second: def __init__(self, first): self.__first = first self.__first.register(self) Now I…
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670