3

I am looking for python stubbing library. Something that could be used to create fake classes/methods in my unit tests.. Is there a simple way to achieve it in python..

Thanks

PS: I am not looking for mocking library where you would record and replay expectation.

Difference between mock and stubs

StackUnderflow
  • 24,080
  • 14
  • 54
  • 77
  • "Mock objects always use behavior verification, a stub can go either way." So a Stub is a Mock? What's the point of saying "no mocks" when "stubs" can do the same thing as mocks? Rather than quote a big article, please provide the actual difference that matters to you. – S.Lott Mar 13 '10 at 02:53

2 Answers2

9

We do this.

class FakeSomethingOrOther( object ):
   def __init__( self ):
       self._count_me= 0
   def method_required_by_test( self ):
       return self.special_answer_required_by_test
   def count_this_method( self, *args, *kw ):
       self._count_me += 1

It doesn't take much to set them up

class TestSomething( unittest.TestCase ):
    def setUp( self ):
        self.requiredSomething = FakeSomethingOrOther()
        self.requiredSomething.attribute_required_by_test= 12
        self.requiredSomething.special_answer_required_by_test = 32
        self.to_be_tested = ActualThing( self.requiredSomething )

Since you don't require complex statically checked type declarations, all you need is a class with the right methods. You can force test attribute values in trivially.

These things are really, really easy to write. You don't need a lot of support or libraries.

In other languages (i.e., Java) it's very hard to write something that will pass muster with static compile-time checking. Since Python doesn't have this problem, it's trivial to write mocks or fake implementations for testing purposes.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

Python mocker looks nice.

A Mocker instance is used to command recording and replaying of expectations on any number of mock objects.

Jacob Schoen
  • 14,034
  • 15
  • 82
  • 102
hyperboreean
  • 8,273
  • 12
  • 61
  • 97
  • 1
    Is it not a mocking framework... where you record expectation and verify them... is there nothing simpler that would just create fake implementation for me.. thanks – StackUnderflow Mar 12 '10 at 22:11
  • 4
    @StackUnderflow: You're splitting some kind of hair. Can you update your question to define a difference between "mocking" and "fake implementation". I think they're the same, but you're saying they're different. – S.Lott Mar 12 '10 at 22:16