Skip to main content

A python mocking framework.

Project description

https://travis-ci.org/atbentley/pock.svg?branch=master https://coveralls.io/repos/github/atbentley/pock/badge.svg?branch=master

Pock is a mocking framework for python. Pock supports:

  • Creating magic mocks
  • Defining the behaviour of mocks
  • Asserting on the access of mocks

Pock does not make assumptions about which testing framework it’s being run inside, so it’ll work nicely with unittest, nosetest, pytest or whatever you choose to use.

Installation

pip install pock

Usage

Create a mock and add some basic behaviour:

from pock import mock, when, verify, any_value

my_mock = mock()
when(my_mock).greet('Andrew').then_return('Hi, Andrew')

my_mock.greet('Andrew')  # 'Hi, Andrew'

Assert on the access of the mock

verify(my_mock).greet('Andrew')  # True
verify(my_mock).greet('Someone else')  # VerificationError

Use matchers to be a little less specific

when(my_mock).start(any_value).then_return(True)
my_mock.start('pock')  # True
verify(my_mock).start(any_value)  # True

Mix and match exact values and matchers

when(my_mock).complex_call(1, 2, any_value, a=4, b=any_value)
my_mock.complex_call(1, 2, 3, a=4, b=5)  # None
verify(my_mock).complex_call(1, any_value, any_valuem a=any_value, b=any_value)

Raise exceptions on mock access

when(my_mock).connect('remotehost').then_raise(ConnectionError('Could not resolve host name'))
my_mock.connect('remotehost')  # ConnectionError

Mocking properties:

property_mock = mock()
when(property_mock).age.then_return(24)
property_mock.age  # 24
verify(property_mock).age  # True

Mocking top level functions:

function_mock = mock()
when(function_mock)('c').then_return(3)
function_mock('c')  # 3
verify(fuction_mock)('c')  # True

Testing

pip install plank
plank install_requirements
plank tests

License

MIT

Project details


Release history Release notifications

History Node

0.0.6

History Node

0.0.5

History Node

0.0.4

This version
History Node

0.0.3

History Node

0.0.2

History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pock-0.0.3.tar.gz (9.7 kB) Copy SHA256 hash SHA256 Source None Oct 22, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page