Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Isolate state on the Braintree sandbox during testing.

Project Description

A Braintree namespace isolates state on the Braintree gateway:

import braintree
import btnamespace

with btnamespace.Namespace():
    customer = braintree.Customer.create({"id": "123"})
    assert == "123"
    braintree.Customer.find("123")  # success

braintree.Customer.find("123")  # NotFound exception

This is primarily useful during integration tests:

def setUp(self):
    self.namespace = btnamespace.Namespace()

def test_some_sandbox_integration(self):

def tearDown(self):

Compared to calling eg braintree.Customer.delete during tearDown, this has a number of advantages:

  • it’s faster, since no teardown is needed
  • it’s simpler, since it doesn’t require any bookkeeping
  • it’s robust, since tests can be written without any state assumptions

You can install it with $ pip install btnamespace.

What’s supported

  • Customer create, update, find, delete
  • CreditCard create, update, find, delete
  • Transaction create, find

All operations involving subresources - eg creating a CreditCard and Customer in one call - work as expected.

Adding support for other operations is easy; we just haven’t needed them yet. Contributions welcome!

How it Works

Under the hood, a Namespace globally patches the braintree client library.

During create operations, any provided ids are removed. This forces the gateway to respond with unique ids, which are later mapped back to the originally-provided ids. Here’s an example:

  • on a call to braintree.Customer.create({'id': '123', ...}), '123' is stored as a Customer id and the call becomes braintree.Customer.create({...}).
  • then, the server returns a unique id 'abcde' for the Customer. '123' is mapped to 'abcde', and the resulting Customer object’s id is set to '123'.
  • later, a call to braintree.Customer.find('123') becomes braintree.Customer.find('abcde').


Inside your vitualenv:

$ cd btnamespace
$ pip install -e .
$ pip install -r requirements.txt

To run the tests, first add your sandbox credentials:

$ export BT_MERCHANT_ID=merchant-id
$ export BT_PUBLIC_KEY=public-id
$ export BT_PRIVATE_KEY=private-key

Then run $ python tests/



released 2016-07-15

  • breaking: drop python 2.6 support
  • fix a bug affecting operations after exiting a namespace
  • add strict_missing and strict_missing_exception options


released 2014-09-26

  • pin bidict to 0.1.5 to avoid breaking changes in newer versions


released 2014-07-28

  • add schema for braintree.ClientToken.generate


released 2014-04-08

  • use less expensive introspection
  • remove decorator dependency


released 2014-04-07

  • initial release

Release History

This version
History Node


History Node


History Node


History Node


History Node


Download Files

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

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(9.4 kB) Copy SHA256 Hash SHA256
Source Jul 15, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting