Mock Stripe Methods in Python for testing

Even though the given answer is correct, there is a way more comfortable solution using vcrpy. That is creating a cassette (record) once a given record does not exist yet. When it does, the mocking is done transparently and the record will be replayed. Beautiful.

Having a vanilla pyramid application, using py.test, my test now looks like this:

import vcr 
# here we have some FactoryBoy fixtures   
from tests.fixtures import PaymentServiceProviderFactory, SSOUserFactory

def test_post_transaction(sqla_session, test_app):
    # first we need a PSP and a User existent in the DB
    psp = PaymentServiceProviderFactory()  # type: PaymentServiceProvider
    user = SSOUserFactory()
    sqla_session.add(psp, user)
    sqla_session.flush()

    with vcr.use_cassette('tests/casettes/tests.checkout.services.transaction_test.test_post_transaction.yaml'):
        # with that PSP we create a new PSPTransaction ...
        res = test_app.post(url='/psps/%s/transaction' % psp.id,
                            params={
                                'token': '4711',
                                'amount': '12.44',
                                'currency': 'EUR',
                            })
        assert 201 == res.status_code
        assert 'id' in res.json_body

This is the right way of doing it:

@mock.patch('stripe.Customer.retrieve')
def test_add_card_failure(self, retrieve_mock):
    data = {
        'name': "shubham",
        'cvc': 123,
        'number': "4242424242424242",
        'expiry': "12/23",
    }
    e = CardError("Card Error", "", "")
    retrieve_mock.return_value.sources.create.return_value = e

    self.api_client.client.login(username=self.username, password=self.password)

    res = self.api_client.post('/biz/api/auth/card/add', data=data)

    self.assertEqual(self.deserialize(res)['success'], False)

IMO, the following method is better than the rest of the answers

import unittest
import stripe
import json
from unittest.mock import patch
from stripe.http_client import RequestsClient # to mock the request session

stripe.api_key = "foo"

stripe.default_http_client = RequestsClient() # assigning the default HTTP client

null = None
false = False
true = True
charge_resp = {
    "id": "ch_1FgmT3DotIke6IEFVkwh2N6Y",
    "object": "charge",
    "amount": 1000,
    "amount_captured": 1000,
    "amount_refunded": 0,
    "billing_details": {
        "address": {
            "city": "Los Angeles",
            "country": "USA",
        },
        "email": null,
        "name": "Jerin",
        "phone": null
    },
    "captured": true,
}


def get_customer_city_from_charge(stripe_charge_id):
    # this is our function and we are writing unit-test for this function
    charge_response = stripe.Charge.retrieve("foo-bar")
    return charge_response.billing_details.address.city


class TestStringMethods(unittest.TestCase):

    @patch("stripe.default_http_client._session")
    def test_get_customer_city_from_charge(self, mock_session):
        mock_response = mock_session.request.return_value
        mock_response.content.decode.return_value = json.dumps(charge_resp)
        mock_response.status_code = 200

        city_name = get_customer_city_from_charge("some_id")
        self.assertEqual(city_name, "Los Angeles")


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

Advantages of this method

  1. You can generate the corresponding class objects (here, the charge_response variable is a type of Charge--(source code))
  2. You can use the dot (.) operator over the response (as we can do with real stripe SDK)
  3. dot operator support for deep attributes