cassette module

cassette.eject(exc_type=None, exc_value=None, tb=None)[source]

Remove cassette, unpatching HTTP requests.

cassette.insert(filename, file_format='')[source]

Setup cassette.

Parameters:filename – path to where requests and responses will be stored.
cassette.play(*args, **kwds)[source]

Use cassette.

cassette_library module

class cassette.cassette_library.CassetteLibrary(filename, encoder, config=None)[source]

The CassetteLibrary holds the stored requests and manage them.

This is an abstract class that needs to have several methods implemented. In addition, subclasses must store request/response pairs as a keys and values as a dictionary in the property self.data

  • filename (str) – filename to use when storing and replaying requests.
  • encoder (Encoder) – the instantiated encodeder to use
add_response(cassette_name, response)[source]

Add a new response to the mocked response.

  • cassette_name (str) –
  • response
cassette_name_for_httplib_connection(host, port, method, url, body, headers)[source]

Create a cassette name from an httplib request.

classmethod create_new_cassette_library(path, file_format, config=None)[source]

Return an instantiated CassetteLibrary.

Use this method to create new a CassetteLibrary. It will automatically determine if it should use a file or directory to back the cassette based on the filename. The method assumes that all file names with an extension (e.g. /file.json) are files, and all file names without extensions are directories (e.g. /requests).

  • path (str) – filename of file or directory for storing requests
  • file_format (str) – the file_format to use for storing requests
  • config (dict) – configuration

Log that a path was used.

report_unused_cassettes(output=<open file '<stdout>', mode 'w'>)[source]

Report unused path to a file.


Restore all responses to a re-seekable state.

save_to_cache(file_hash, data)[source]

Save a decoded data object into cache.


Write the response data to file.

class cassette.cassette_library.CassetteName[source]

A CassetteName represents an unique way to retrieve the cassette from the library.

classmethod from_httplib_connection(host, port, method, url, body, headers, will_hash_body=False)[source]

Create an object from an httplib request.

class cassette.cassette_library.DirectoryCassetteLibrary(*args, **kwargs)[source]

A CassetteLibrary that stores and manages requests with directory.

cassette_name_for_httplib_connection(host, port, method, url, body, headers)[source]

Create a cassette name from an httplib request.


Generate the filename for a given cassette name.


Generate the full path to cassette file.


Return all available cassette.


Write mocked response to a directory of files.

class cassette.cassette_library.FileCassetteLibrary(filename, encoder, config=None)[source]

Store and manage requests with a single file.


Lazily loaded data.


Return all available cassette.


Load MockedResponses from YAML file.


Write mocked responses to file.

patcher module


Replace standard library.


Unpatch standard library.

unpatched module

cassette.unpatched.unpatched_httplib_context(*args, **kwds)[source]

Create a context in which httplib is unpatched.