Message Pact¶
API for creating a contract and configuring the mock service.
Attributes¶
Classes¶
MessagePact(consumer, provider, publish_to_broker=False, broker_base_url=None, broker_username=None, broker_password=None, broker_token=None, pact_dir=None, version='3.0.0', file_write_mode='merge')
¶
Bases: Broker
Represents a contract between a consumer and provider using messages.
Provides Python context handler to perform tests on a Python consumer. For example:
from pact import MessageConsumer, Provider pact = MessageConsumer('MyMessageConsumer').has_pact_with(Provider('provider')) (pact ... .given({"name": "Test provider"}]) ... .expects_to_receive('Test description') ... .with_content({'name': 'John', 'document_name': 'sample_document.doc'}) ... .with_metadata({'contentType': 'application/json'})) with pact: ... handler(event, context)
:param consumer: A consumer for this contract that uses messages.
:type consumer: pact.MessageConsumer
:param provider: The generic provider for this contract.
:type provider: pact.Provider
:param publish_to_broker: Flag to control automatic publishing of
pacts to a pact broker. Defaults to False.
:type publish_to_broker: bool
:param broker_base_url: URL of the pact broker that pacts will be
published to. Can also be supplied through the PACT_BROKER_BASE_URL
environment variable. Defaults to None.
:type broker_base_url: str
:param broker_username: Username to use when connecting to the pact
broker if authentication is required. Can also be supplied through
the PACT_BROKER_USERNAME environment variable. Defaults to None.
:type broker_username: str
:param broker_password: Password to use when connecting to the pact
broker if authentication is required. Strongly recommend supplying
this value through the PACT_BROKER_PASSWORD environment variable
instead. Defaults to None.
:type broker_password: str
:param broker_token: Authentication token to use when connecting to
the pact broker. Strongly recommend supplying this value through
the PACT_BROKER_TOKEN environment variable instead.
Defaults to None.
:type broker_token: str
:param pact_dir: Directory where the resulting pact files will be
written. Defaults to the current directory.
:type pact_dir: str
:param version: The Pact Specification version to use, defaults to
'3.0.0'.
:type version: str
:param file_write_mode: overwrite
or merge
. Use merge
when
running multiple mock service instances in parallel for the same
consumer/provider pair. Ensure the pact file is deleted before
running tests when using this option so that interactions deleted
from the code are not maintained in the file. Defaults to
merge
.
:type file_write_mode: str
Source code in src/pact/message_pact.py
Attributes¶
MANDATORY_FIELDS = {'providerStates', 'description', 'contents', 'metaData'}
class-attribute
instance-attribute
¶
consumer = consumer
instance-attribute
¶
file_write_mode = file_write_mode
instance-attribute
¶
pact_dir = pact_dir or os.getcwd()
instance-attribute
¶
provider = provider
instance-attribute
¶
publish_to_broker = publish_to_broker
instance-attribute
¶
version = version
instance-attribute
¶
Functions¶
expects_to_receive(description)
¶
Define the name of this contract (Same as upon_receiving in http pact implementation).
:param scenario: A unique name for this contract. :type scenario: basestring :rtype: Pact
Source code in src/pact/message_pact.py
given(name, params=None)
¶
Define the provider state for this pact.
When the provider verifies this contract, they will use this field to setup pre-defined data that will satisfy the response expectations.
:param name: The short sentence that is unique to describe the provider state for this contract. :type name: basestring :param params: Additional arguments necessary to set the provider state :type params: dict :rtype: Pact
Source code in src/pact/message_pact.py
with_content(contents)
¶
Define message content (event) that will be use in the message.
:param contents: dictionary of dictionary used in the message. :type metadata: dict :rtype: Pact
Source code in src/pact/message_pact.py
with_metadata(metadata)
¶
Define metadata attached to the message.
:param metadata: dictionary of metadata attached to the message. :type metadata: dict or None :rtype: Pact
Source code in src/pact/message_pact.py
write_to_pact_file()
¶
Create a pact file based on provided attributes in DSL.
Return 0 if success, 1 otherwise.
:rtype: int