Pact¶
Python methods for interactive with a Pact Mock Service.
Attributes¶
SomethingLike = Like
module-attribute
¶
Classes¶
Broker(broker_base_url=None, broker_username=None, broker_password=None, broker_token=None)
¶
PactBroker helper functions.
: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.
Source code in src/pact/broker.py
Attributes¶
broker_base_url = broker_base_url
instance-attribute
¶
broker_password = broker_password
instance-attribute
¶
broker_token = broker_token
instance-attribute
¶
broker_username = broker_username
instance-attribute
¶
Functions¶
publish(consumer_name, version, pact_dir=None, tag_with_git_branch=None, consumer_tags=None, branch=None, build_url=None, auto_detect_version_properties=None)
¶
Publish the generated pact files to the specified pact broker.
Source code in src/pact/broker.py
Consumer(name, service_cls=Pact, tags=None, tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None, auto_detect_version_properties=False)
¶
Bases: object
A Pact consumer.
Use this class to describe the service making requests to the provider and
then use has_pact_with
to create a contract with a specific service:
from pact import Consumer, Provider consumer = Consumer('my-web-front-end') consumer.has_pact_with(Provider('my-backend-service'))
:param name: The name of this Consumer. This will be shown in the Pact when it is published. :type name: str :param service_cls: Pact, or a sub-class of it, to use when creating the contracts. This is useful when you have a custom URL or port for your mock service and want to use the same value on all of your contracts. :type service_cls: pact.Pact :param tags: A list of strings to use as tags to use when publishing to a pact broker. Defaults to None. :type tags: list :param tag_with_git_branch: A flag to determine whether to automatically tag a pact with the current git branch name. Defaults to False. :type tag_with_git_branch: bool :param version: The version of this Consumer. This will be used when publishing pacts to a pact broker. Defaults to '0.0.0' :param branch: The branch of this Consumer. :type branch: str :param build_url: The build URL that created the pact. :type build_url: str :param auto_detect_version_properties: Automatically detect the repository branch from known CI, environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'. Defaults to False. :type auto_detect_version_properties: bool
Source code in src/pact/consumer.py
Attributes¶
auto_detect_version_properties = auto_detect_version_properties
instance-attribute
¶
branch = branch
instance-attribute
¶
build_url = build_url
instance-attribute
¶
name = name
instance-attribute
¶
service_cls = service_cls
instance-attribute
¶
tag_with_git_branch = tag_with_git_branch
instance-attribute
¶
tags = tags
instance-attribute
¶
version = version
instance-attribute
¶
Functions¶
has_pact_with(provider, host_name='localhost', port=1234, log_dir=None, ssl=False, sslcert=None, sslkey=None, cors=False, publish_to_broker=False, broker_base_url=None, broker_username=None, broker_password=None, broker_token=None, pact_dir=None, specification_version='2.0.0', file_write_mode='overwrite')
¶
Create a contract between the provider
and this consumer.
If you are running the Pact mock service in a non-default location, you can provide the host name and port here:
from pact import Consumer, Provider consumer = Consumer('my-web-front-end') consumer.has_pact_with( ... Provider('my-backend-service'), ... host_name='192.168.1.1', ... port=8000)
:param provider: The provider service for this contract.
:type provider: pact.Provider
:param host_name: An optional host name to use when contacting the
Pact mock service. This will need to be the same host name used by
your code under test to contact the mock service. It defaults to:
localhost
.
:type host_name: str
:param port: The TCP port to use when contacting the Pact mock service.
This will need to be the same port used by your code under test
to contact the mock service. It defaults to: 1234
:type port: int
:param log_dir: The directory where logs should be written. Defaults to
the current directory.
:type log_dir: str
:param ssl: Flag to control the use of a self-signed SSL cert to run
the server over HTTPS , defaults to False.
:type ssl: bool
:param sslcert: Path to a custom self-signed SSL cert file, 'ssl'
option must be set to True to use this option. Defaults to None.
:type sslcert: str
:param sslkey: Path to a custom key and self-signed SSL cert key file,
'ssl' option must be set to True to use this option.
Defaults to None.
:type sslkey: str
:param cors: Allow CORS OPTION requests to be accepted,
defaults to False.
:type cors: bool
: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. Defaults to None.
:type broker_base_url: str
:param broker_username: Username to use when connecting to the pact
broker if authentication is required. Defaults to None.
:type broker_username: str
:param broker_password: Password to use when connecting to the pact
broker if authentication is required. Defaults to None.
:type broker_password: str
:param broker_token: Authentication token to use when connecting to
the pact broker. 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 specification_version: The Pact Specification version to use.
Defaults to '2.0.0'.
:type version: str
:param file_write_mode: How the mock service should apply multiple
calls to .verify(). Pass 'overwrite' to overwrite the generated
JSON file on every call to .verify() or pass 'merge' to merge all
interactions into the same JSON file. When using 'merge', make
sure to delete any existing JSON file before calling .verify()
for the first time. Defaults to 'overwrite'.
:type version: str
:return: A Pact object which you can use to define the specific
interactions your code will have with the provider.
:rtype: pact.Pact
Source code in src/pact/consumer.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
EachLike(matcher, minimum=1)
¶
Bases: Matcher
Expect the data to be a list of similar objects.
Example:
from pact import Consumer, Provider pact = Consumer('consumer').has_pact_with(Provider('provider')) (pact.given('there are three comments') ... .upon_receiving('a request for the most recent 2 comments') ... .with_request('get', '/comment', query={'limit': 2}) ... .will_respond_with(200, body={ ... 'comments': EachLike( ... {'name': SomethingLike('bob'), ... 'text': SomethingLike('Hello!')}, ... minimum=2) ... })) Would expect the response to be a JSON object, with a comments list. In that list should be at least 2 items, and each item should be a
dict
with the keysname
andtext
,
:param matcher: The expected value that each item in a list should look like, this can be other matchers. :type matcher: None, list, dict, int, float, str, unicode, Matcher :param minimum: The minimum number of items expected. Must be greater than or equal to 1. :type minimum: int
Source code in src/pact/matchers.py
Attributes¶
matcher = matcher
instance-attribute
¶
minimum = minimum
instance-attribute
¶
Functions¶
generate()
¶
Generate the value the mock service will return.
:return: A dict containing the information about the contents of the list and the provided minimum number of items for that list. :rtype: dict
Source code in src/pact/matchers.py
Format()
¶
Class of regular expressions for common formats.
Example:
from pact import Consumer, Provider from pact.matchers import Format pact = Consumer('consumer').has_pact_with(Provider('provider')) (pact.given('the current user is logged in as
tester
') ... .upon_receiving('a request for the user profile') ... .with_request('get', '/profile') ... .will_respond_with(200, body={ ... 'id': Format().identifier, ... 'lastUpdated': Format().time ... }))
Would expect id
to be any valid int and lastUpdated
to be a valid time.
When the consumer runs this contract, the value of that will be returned
is the second value passed to Term in the given function, for the time
example it would be datetime.datetime(2000, 2, 1, 12, 30, 0, 0).time()
Source code in src/pact/matchers.py
Attributes¶
identifier = self.integer_or_identifier()
instance-attribute
¶
integer = self.integer_or_identifier()
instance-attribute
¶
iso_datetime = self.iso_8601_datetime()
instance-attribute
¶
iso_datetime_ms = self.iso_8601_datetime(with_ms=True)
instance-attribute
¶
Classes¶
Regexes
¶
Bases: Enum
Regex Enum for common formats.
Attributes¶
date = '^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))?)'
class-attribute
instance-attribute
¶hexadecimal = '[0-9a-fA-F]+'
class-attribute
instance-attribute
¶ip_address = '(\\d{1,3}\\.)+\\d{1,3}'
class-attribute
instance-attribute
¶ipv6_address = '(\\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\\Z)|(\\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\\Z)|(\\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\\Z)|(\\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\\Z)|(\\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\\Z)|(\\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}\\Z)|(\\A(([0-9a-f]{1,4}:){1,7}|:):\\Z)|(\\A:(:[0-9a-f]{1,4}){1,7}\\Z)|(\\A((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3})\\Z)|(\\A(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3})\\Z)|(\\A([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|(\\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|(\\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|(\\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|(\\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|(\\A(([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)|(\\A:(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\Z)'
class-attribute
instance-attribute
¶iso_8601_datetime = '^\\d{4}-[01]\\d-[0-3]\\d\\x54[0-2]\\d:[0-6]\\d:[0-6]\\d(?:\\.\\d+)?(?:(?:[+-]\\d\\d:?\\d\\d)|\\x5A)?$'
class-attribute
instance-attribute
¶iso_8601_datetime_ms = '^\\d{4}-[01]\\d-[0-3]\\d\\x54[0-2]\\d:[0-6]\\d:[0-6]\\d\\.\\d+(?:(?:[+-]\\d\\d:?\\d\\d)|\\x5A)?$'
class-attribute
instance-attribute
¶time_regex = '^(T\\d\\d:\\d\\d(:\\d\\d)?(\\.\\d+)?(([+-]\\d\\d:\\d\\d)|Z)?)?$'
class-attribute
instance-attribute
¶timestamp = '^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$'
class-attribute
instance-attribute
¶uuid = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
class-attribute
instance-attribute
¶Functions¶
date()
¶
Match any date.
:return: a Term object with a date regex. :rtype: Term
decimal()
¶
hexadecimal()
¶
Match any hexadecimal.
:return: a Term object with a hexadecimal regex. :rtype: Term
integer_or_identifier()
¶
ip_address()
¶
Match any ip address.
:return: a Term object with an ip address regex. :rtype: Term
ipv6_address()
¶
Match any ipv6 address.
:return: a Term object with an ipv6 address regex. :rtype: Term
iso_8601_datetime(with_ms=False)
¶
Match a string for a full ISO 8601 Date.
Does not do any sort of date validation, only checks if the string is according to the ISO 8601 spec.
This method differs from :func:~pact.Format.timestamp
,
:func:~pact.Format.date
and :func:~pact.Format.time
implementations
in that it is more stringent and tests the string for exact match to
the ISO 8601 dates format.
Without with_ms
will match string containing ISO 8601 formatted dates
as stated bellow:
- 2016-12-15T20:16:01
- 2010-05-01T01:14:31.876
- 2016-05-24T15:54:14.00000Z
- 1994-11-05T08:15:30-05:00
- 2002-01-31T23:00:00.1234-02:00
- 1991-02-20T06:35:26.079043+00:00
Otherwise, ONLY dates with milliseconds will match the pattern:
- 2010-05-01T01:14:31.876
- 2016-05-24T15:54:14.00000Z
- 2002-01-31T23:00:00.1234-02:00
- 1991-02-20T06:35:26.079043+00:00
:param with_ms: Enforcing millisecond precision. :type with_ms: bool :return: a Term object with a date regex. :rtype: Term
Source code in src/pact/matchers.py
time()
¶
Match any time.
:return: a Term object with a time regex. :rtype: Term
timestamp()
¶
Match any timestamp.
:return: a Term object with a timestamp regex. :rtype: Term
uuid()
¶
Match any uuid.
:return: a Term object with a uuid regex. :rtype: Term
Like(matcher)
¶
Bases: Matcher
Expect the type of the value to be the same as matcher.
Example:
from pact import Consumer, Provider pact = Consumer('consumer').has_pact_with(Provider('provider')) (pact ... .given('there is a random number generator') ... .upon_receiving('a request for a random number') ... .with_request('get', '/generate-number') ... .will_respond_with(200, body={ ... 'number': Like(1111222233334444) ... }))
Would expect the response body to be a JSON object, containing the key
number
, which would contain an integer. When the consumer runs this
contract, the value 1111222233334444
will be returned by the mock
service, instead of a randomly generated value.
:param matcher: The object that should be expected. The mock service will return this value. When verified against the provider, the type of this value will be asserted, while the value will be ignored. :type matcher: None, list, dict, int, float, str, unicode, Matcher
Source code in src/pact/matchers.py
MessageConsumer(name, service_cls=MessagePact, tags=None, tag_with_git_branch=False, version='0.0.0', branch=None, build_url=None, auto_detect_version_properties=False)
¶
Bases: object
A Pact message consumer.
Use this class to describe the service making requests to the provider and
then use has_pact_with
to create a contract with a specific service:
from pact import MessageConsumer, Provider message_consumer = MessageConsumer('my-web-front-end') message_consumer.has_pact_with(Provider('my-backend-service'))
:param name: The name of this Consumer. This will be shown in the Pact when it is published. :type name: str :param service_cls: Pact, or a sub-class of it, to use when creating the contracts. This is useful when you have a custom URL or port for your mock service and want to use the same value on all of your contracts. :type service_cls: pact.Pact :param tags: A list of strings to use as tags to use when publishing to a pact broker. Defaults to None. :type tags: list :param tag_with_git_branch: A flag to determine whether to automatically tag a pact with the current git branch name. Defaults to False. :type tag_with_git_branch: bool :param version: The version of this Consumer. This will be used when publishing pacts to a pact broker. Defaults to '0.0.0' :param branch: The branch of this Consumer. :type branch: str :param build_url: The build URL that created the pact. :type build_url: str :param auto_detect_version_properties: Automatically detect the repository branch from known CI, environment variables or git CLI. Supports Buildkite, Circle CI, Travis CI, GitHub Actions, Jenkins, Hudson, AppVeyor, GitLab, CodeShip, Bitbucket and Azure DevOps.'. Defaults to False. :type auto_detect_version_properties: bool
Source code in src/pact/message_consumer.py
Attributes¶
auto_detect_version_properties = auto_detect_version_properties
instance-attribute
¶
branch = branch
instance-attribute
¶
build_url = build_url
instance-attribute
¶
name = name
instance-attribute
¶
service_cls = service_cls
instance-attribute
¶
tag_with_git_branch = tag_with_git_branch
instance-attribute
¶
tags = tags
instance-attribute
¶
version = version
instance-attribute
¶
Functions¶
has_pact_with(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')
¶
Create a contract between the provider
and this consumer.
If you are running the Pact mock service in a non-default location, you can provide the host name and port here:
from pact import Consumer, Provider consumer = Consumer('my-web-front-end') consumer.has_pact_with( ... Provider('my-backend-service'), ... host_name='192.168.1.1', ... port=8000)
:param provider: The provider service for this contract.
:type provider: pact.Provider
:param host_name: An optional host name to use when contacting the
Pact mock service. This will need to be the same host name used by
your code under test to contact the mock service. It defaults to:
localhost
.
:type host_name: str
: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. Defaults to None.
:type broker_base_url: str
:param broker_username: Username to use when connecting to the pact
broker if authentication is required. Defaults to None.
:type broker_username: str
:param broker_password: Password to use when connecting to the pact
broker if authentication is required. Defaults to None.
:type broker_password: str
:param broker_token: Authentication token to use when connecting to
the pact broker. 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: How the mock service should apply multiple
calls to .verify(). Pass 'overwrite' to overwrite the generated
JSON file on every call to .verify() or pass 'merge' to merge all
interactions into the same JSON file. When using 'merge', make
sure to delete any existing JSON file before calling .verify()
for the first time. Defaults to 'merge'.
:type version: str
:return: A Pact object which you can use to define the specific
interactions your code will have with the provider.
:rtype: pact.Pact
Source code in src/pact/message_consumer.py
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
Source code in src/pact/message_pact.py
MessageProvider(message_providers, provider, consumer, pact_dir=os.getcwd(), version='3.0.0', proxy_host='localhost', proxy_port='1234')
¶
Bases: object
A Pact message provider.
provider = MessageProvider( message_providers = { "a document created successfully": handler }, provider='DocumentService', pact_dir='pacts', version='3.0.0' )
Source code in src/pact/message_provider.py
Attributes¶
consumer = consumer
instance-attribute
¶
message_providers = message_providers
instance-attribute
¶
pact_dir = pact_dir
instance-attribute
¶
provider = provider
instance-attribute
¶
proxy_host = proxy_host
instance-attribute
¶
proxy_port = proxy_port
instance-attribute
¶
version = version
instance-attribute
¶
Functions¶
verify()
¶
Verify pact files with executable verifier.
Source code in src/pact/message_provider.py
verify_with_broker(enable_pending=False, include_wip_pacts_since=None, **kwargs)
¶
Use Broker to verify.
PARAMETER | DESCRIPTION |
---|---|
broker_username
|
broker username
TYPE:
|
broker_password
|
broker password
TYPE:
|
broker_url
|
url of broker
TYPE:
|
Source code in src/pact/message_provider.py
Pact(consumer, provider, host_name='localhost', port=1234, log_dir=None, ssl=False, sslcert=None, sslkey=None, cors=False, publish_to_broker=False, broker_base_url=None, broker_username=None, broker_password=None, broker_token=None, pact_dir=None, specification_version='2.0.0', file_write_mode='overwrite')
¶
Bases: Broker
Represents a contract between a consumer and provider.
Provides Python context handlers to configure the Pact mock service to perform tests on a Python consumer. For example:
from pact import Consumer, Provider pact = Consumer('consumer').has_pact_with(Provider('provider')) (pact.given('the echo service is available') ... .upon_receiving('a request is made to the echo service') ... .with_request('get', '/echo', query={'text': 'Hello!'}) ... .will_respond_with(200, body='Hello!')) with pact: ... requests.get(pact.uri + '/echo?text=Hello!')
The GET request is made to the mock service, which will verify that it
was a GET to /echo with a query string with a key named text
and its
value is Hello!
. If the request does not match an error is raised, if it
does match the defined interaction, it will respond with the text Hello!
.
:param consumer: The consumer for this contract.
:type consumer: pact.Consumer
:param provider: The provider for this contract.
:type provider: pact.Provider
:param host_name: The host name where the mock service is running.
:type host_name: str
:param port: The port number where the mock service is running.
:type port: int
:param log_dir: The directory where logs should be written. Defaults to
the current directory.
:type log_dir: str
:param ssl: Flag to control the use of a self-signed SSL cert to run
the server over HTTPS , defaults to False.
:type ssl: bool
:param sslcert: Path to a custom self-signed SSL cert file, 'ssl'
option must be set to True to use this option. Defaults to None.
:type sslcert: str
:param sslkey: Path to a custom key and self-signed SSL cert key file,
'ssl' option must be set to True to use this option.
Defaults to None.
:type sslkey: str
:param cors: Allow CORS OPTION requests to be accepted,
defaults to False.
:type cors: bool
: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 specification_version: The Pact Specification version to use, defaults to
'2.0.0'.
:type version: str of the consumer version.
: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
overwrite
.
:type file_write_mode: str
Source code in src/pact/pact.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
Attributes¶
HEADERS = {'X-Pact-Mock-Service': 'true'}
class-attribute
instance-attribute
¶
MANDATORY_FIELDS = {'response', 'description', 'request'}
class-attribute
instance-attribute
¶
consumer = consumer
instance-attribute
¶
cors = cors
instance-attribute
¶
file_write_mode = file_write_mode
instance-attribute
¶
host_name = host_name
instance-attribute
¶
log_dir = log_dir or os.getcwd()
instance-attribute
¶
pact_dir = pact_dir or os.getcwd()
instance-attribute
¶
port = port
instance-attribute
¶
provider = provider
instance-attribute
¶
publish_to_broker = publish_to_broker
instance-attribute
¶
specification_version = specification_version
instance-attribute
¶
ssl = ssl
instance-attribute
¶
sslcert = sslcert
instance-attribute
¶
sslkey = sslkey
instance-attribute
¶
uri = '{scheme}://{host_name}:{port}'.format(host_name=host_name, port=port, scheme=scheme)
instance-attribute
¶
Functions¶
given(provider_state)
¶
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 provider_state: The short sentence that is unique to describe the provider state for this contract. :type provider_state: basestring :rtype: Pact
Source code in src/pact/pact.py
setup()
¶
Configure the Mock Service to ready it for a test.
Source code in src/pact/pact.py
start_service()
¶
Start the external Mock Service.
:raises RuntimeError: if there is a problem starting the mock service.
Source code in src/pact/pact.py
stop_service()
¶
Stop the external Mock Service.
Source code in src/pact/pact.py
upon_receiving(scenario)
¶
Define the name of this contract.
:param scenario: A unique name for this contract. :type scenario: basestring :rtype: Pact
Source code in src/pact/pact.py
verify()
¶
Have the mock service verify all interactions occurred.
Calls the mock service to verify that all interactions occurred as expected, and has it write out the contracts to disk.
:raises AssertionError: When not all interactions are found.
Source code in src/pact/pact.py
will_respond_with(status, headers=None, body=None)
¶
Define the response the server is expected to create.
:param status: The HTTP status code. :type status: int :param headers: All required headers. Defaults to None. :type headers: dict or None :param body: The response body, or a collection of Matcher objects to allow for pattern matching. Defaults to None. :type body: Matcher, dict, list, basestring, or None :rtype: Pact
Source code in src/pact/pact.py
with_request(method, path, body=None, headers=None, query=None)
¶
Define the request that the client is expected to perform.
:param method: The HTTP method. :type method: str :param path: The path portion of the URI the client will access. :type path: str, Matcher :param body: The request body, can be a string or an object that will serialize to JSON, like list or dict, defaults to None. :type body: list, dict or None :param headers: The headers the client is expected to include on with this request. Defaults to None. :type headers: dict or None :param query: The query options the client is expected to send. Can be a dict of keys and values, or a URL encoded string. Defaults to None. :type query: dict, basestring, or None :rtype: Pact
Source code in src/pact/pact.py
Provider(name)
¶
Bases: object
A Pact provider.
:param name: The name of this provider. This will be shown in the Pact when it is published. :type name: str
Source code in src/pact/provider.py
Term(matcher, generate)
¶
Bases: Matcher
Expect the response to match a specified regular expression.
Example:
from pact import Consumer, Provider pact = Consumer('consumer').has_pact_with(Provider('provider')) (pact.given('the current user is logged in as
tester
') ... .upon_receiving('a request for the user profile') ... .with_request('get', '/profile') ... .will_respond_with(200, body={ ... 'name': 'tester', ... 'theme': Term('light|dark|legacy', 'dark') ... }))
Would expect the response body to be a JSON object, containing the key
name
, which will contain the value tester
, and theme
which must be
one of the values: light, dark, or legacy. When the consumer runs this
contract, the value dark
will be returned by the mock service.
:param matcher: A regular expression to find. :type matcher: basestring :param generate: A value to be returned by the mock service when generating the response to the consumer. :type generate: basestring
Source code in src/pact/matchers.py
Attributes¶
matcher = matcher
instance-attribute
¶
Functions¶
generate()
¶
Return the value that should be used in the request/response.
:return: A dict containing the information about what the contents of the response should be, and what should match for the requests. :rtype: dict
Source code in src/pact/matchers.py
Verifier(provider, provider_base_url, **kwargs)
¶
Bases: object
A Pact Verifier.
PARAMETER | DESCRIPTION |
---|---|
provider
|
provider name
TYPE:
|
provider_base_url
|
provider url
TYPE:
|
Source code in src/pact/verifier.py
Attributes¶
provider = provider
instance-attribute
¶
provider_base_url = provider_base_url
instance-attribute
¶
Functions¶
extract_params(**kwargs)
¶
Extract params.
Source code in src/pact/verifier.py
filter_empty_options(**kwargs)
¶
Filter out empty options.
Source code in src/pact/verifier.py
is_empty_list(item)
¶
validate_publish(**kwargs)
¶
verify_pacts(*pacts, enable_pending=False, include_wip_pacts_since=None, **kwargs)
¶
Verify our pacts from the provider.
RETURNS | DESCRIPTION |
---|---|
success
|
True if no failures |
logs
|
some tbd output of logs |
Source code in src/pact/verifier.py
verify_with_broker(enable_pending=False, include_wip_pacts_since=None, **kwargs)
¶
Use Broker to verify.
PARAMETER | DESCRIPTION |
---|---|
broker_username
|
broker username
TYPE:
|
broker_password
|
broker password
TYPE:
|
broker_url
|
url of broker
TYPE:
|