Module pmock
[show private | hide private]
[frames | no frames]

Module pmock

Python mock object framework, providing support for creating mock objects for use in unit testing.

The api is modelled on the jmock mock object framework.

Usage:
   import pmock
   import unittest

   class PowerStation(object):
       def start_up(self, reactor):
           try:
               reactor.activate('core')
           except Exception, err:
               reactor.shutdown()

   class PowerStationTestCase(unittest.TestCase):
       def test_successful_activation(self):
           mock = pmock.Mock()
           mock.expects(pmock.once()).activate(pmock.eq('core'))
           PowerStation().start_up(mock)
           mock.verify()
       def test_problematic_activation(self):
           mock = pmock.Mock()
           mock.expects(pmock.once()).activate(pmock.eq('core')).will(
               pmock.throw_exception(RuntimeError('overheating')))
           mock.expects(pmock.once()).shutdown()
           PowerStation().start_up(mock)
           mock.verify()

   if __name__ == '__main__':
       unittest.main()

Further information is available in the bundled documentation, and from http://pmock.sourceforge.net/

Copyright (c) 2004, Graham Carlyle

This module is free software, and you may redistribute it and/or modify it under the same terms as Python itself, so long as this copyright message and disclaimer are retained in their original form.

IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Classes
Mock A mock object.
MockTestCase  
AbstractArgumentsMatcher  
AllArgumentsMatcher  
AtLeastOnceInvocationMatcher  
BoundMethod  
DefaultStub  
EqConstraint  
FunctorConstraint  
Invocation  
InvocationMocker  
InvocationMockerBuilder  
InvokedAfterMatcher  
InvokedRecorderMatcher  
LeastArgumentsMatcher  
MethodMatcher  
NotCalledInvocationMatcher  
OnceInvocationMatcher  
Proxy A proxy for a mock object.
RaiseExceptionStub  
ReturnValueStub  
SameConstraint  
StringContainsConstraint  
StubInvocationMatcher  

Exceptions
DefinitionError Expectation definition isn't valid.
Error  
MatchError Method call unexpected.
VerificationError An expectation have failed verification.

Function Summary
  at_least_once()
Method will be called at least once.
  eq(expected)
Argument will be equal to supplied value.
  functor(boolean_functor)
Supplied unary function evaluates to True when called with argument.
  never()
Method will not be called.
  once()
Method will be called only once.
  raise_exception(exception)
Stub that raises the supplied exception.
  return_value(value)
Stub that returns the supplied value.
  same(expected)
Argument will be the same as the supplied reference.
  string_contains(expected)
Argument contains the supplied substring.

Variable Summary
str __author__ = 'Graham Carlyle'
str __email__ = 'grahamcarlyle at users dot sourceforge dot ...
str __version__ = '0.3'
DefaultStub _DEFAULT_STUB = <pmock.DefaultStub object at 0x403bd1cc>
NotCalledInvocationMatcher _NOT_CALLED_MATCHER_INSTANCE = <pmock.NotCalledInvocatio...
StubInvocationMatcher _STUB_MATCHER_INSTANCE = <pmock.StubInvocationMatcher ob...
LeastArgumentsMatcher ANY_ARGS_MATCHER = <pmock.LeastArgumentsMatcher object a...
AllArgumentsMatcher NO_ARGS_MATCHER = <pmock.AllArgumentsMatcher object at 0...

Function Details

at_least_once()

Method will be called at least once.

Convenience function for creating a AtLeastOnceInvocationMatcher instance.

eq(expected)

Argument will be equal to supplied value.

Convenience function for creating a EqConstraint instance.

functor(boolean_functor)

Supplied unary function evaluates to True when called with argument.

Convenience function for creating a FunctorConstraint instance.

never()

Method will not be called.

Convenience function for getting a NotCalledInvocationMatcher instance.

once()

Method will be called only once.

Convenience function for creating a OnceInvocationMatcher instance.

raise_exception(exception)

Stub that raises the supplied exception.

Convenience function for creating a RaiseExceptionStub instance.

return_value(value)

Stub that returns the supplied value.

Convenience function for creating a ReturnValueStub instance.

same(expected)

Argument will be the same as the supplied reference.

Convenience function for creating a SameConstraint instance.

string_contains(expected)

Argument contains the supplied substring.

Convenience function for creating a StringContainsConstraint instance.

Variable Details

__author__

Type:
str
Value:
'Graham Carlyle'                                                       

__email__

Type:
str
Value:
'grahamcarlyle at users dot sourceforge dot net'                       

__version__

Type:
str
Value:
'0.3'                                                                  

_DEFAULT_STUB

Type:
DefaultStub
Value:
<pmock.DefaultStub object at 0x403bd1cc>                               

_NOT_CALLED_MATCHER_INSTANCE

Type:
NotCalledInvocationMatcher
Value:
<pmock.NotCalledInvocationMatcher object at 0x403bd4ac>                

_STUB_MATCHER_INSTANCE

Type:
StubInvocationMatcher
Value:
<pmock.StubInvocationMatcher object at 0x403bd4ec>                     

ANY_ARGS_MATCHER

Type:
LeastArgumentsMatcher
Value:
<pmock.LeastArgumentsMatcher object at 0x403a5f0c>                     

NO_ARGS_MATCHER

Type:
AllArgumentsMatcher
Value:
<pmock.AllArgumentsMatcher object at 0x403a5f2c>                       

Generated by Epydoc 2.0 on Sun Jul 25 19:39:43 2004 http://epydoc.sf.net