Verifier¶
Verifier for Pact.
The Verifier is used to verify that a provider meets the expectations of a consumer. This is done by replaying interactions from the consumer against the provider, and ensuring that the provider's responses match the expectations set by the consumer.
The interactions to be verified can be sourced either from local Pact files or from a Pact Broker. The Verifier can be configured to filter interactions based on their description and state, and to set the provider information and transports.
When performing the verification, Pact will replay the interactions from the consumer against the provider and ensure that the provider's responses match the expectations set by the consumer.
Info
The interface provided by this module could be improved. If you have any suggestions, please consider creating a new GitHub discussion or reaching out over Slack.
Usage¶
The general usage of the Verifier is as follows:
from pact.v3 import Verifier
# In the case of local Pact files
verifier = Verifier().set_info("My Provider", url="http://localhost:8080")
verifier.add_source("pact/to/pacts/")
verifier.verify()
# In the case of a Pact Broker
verifier = Verifier().set_info("My Provider", url="http://localhost:8080")
verifier.broker_source("https://broker.example.com/")
verifier.verify()
State Handling¶
In general, the consumer will write interactions assuming that the provider is
in a certain state. For example, a consumer requesting information about a user
with ID 123
will have specified given("user with ID 123 exists")
. It is the
responsibility of the provider to ensure that this state is met before the
interaction is replayed.
In order to change the provider's internal state, Pact relies on a callback endpoint. The specific manner in which this endpoint is implemented is up to the provider as it is highly dependent on the provider's architecture.
One common approach is to define the endpoint during testing only, and for the endpoint to mock the expected calls to the database and/or external services. This allows the provider to be tested in isolation from the rest of the system, and assertions can be made about the calls made to the endpoint.
An alternative approach might be to run a dedicated service which is responsible for writing to the database such that the provider can retrieve the expected data. This approach is more complex, but could be useful in cases where test databases are already in use.
Classes¶
BrokerSelectorBuilder(verifier: Verifier, url: str, username: str | None, password: str | None, token: str | None)
¶
A Broker selector.
This class encapsulates the logic for selecting Pacts from a Pact broker.
This constructor should not be called directly. Instead, use the
broker_source
method of the Verifier
class with selector=True
.
Source code in src/pact/v3/verifier.py
Functions¶
build() -> Verifier
¶
Build the Broker Selector.
RETURNS | DESCRIPTION |
---|---|
Verifier
|
The Verifier instance with the broker source added. |
Source code in src/pact/v3/verifier.py
consumer_tags(*tags: str) -> Self
¶
consumer_versions(*versions: str) -> Self
¶
exclude_pending() -> Self
¶
exclude_wip() -> Self
¶
include_pending() -> Self
¶
include_wip_since(d: str | date) -> Self
¶
Include work in progress Pacts since a given date.
provider_branch(branch: str) -> Self
¶
Verifier()
¶
A Verifier between a consumer and a provider.
This class encapsulates the logic for verifying that a provider meets the expectations of a consumer. This is done by replaying interactions from the consumer against the provider, and ensuring that the provider's responses match the expectations set by the consumer.
Source code in src/pact/v3/verifier.py
Attributes¶
logs: str
property
¶
Get the logs.
results: dict[str, Any]
property
¶
Get the results.
Functions¶
add_custom_header(name: str, value: str) -> Self
¶
Add a customer header to the request.
These headers are added to every request made to the provider.
PARAMETER | DESCRIPTION |
---|---|
name
|
The key of the header.
TYPE:
|
value
|
The value of the header.
TYPE:
|
Source code in src/pact/v3/verifier.py
add_custom_headers(headers: dict[str, str] | Iterable[tuple[str, str]]) -> Self
¶
Add multiple customer headers to the request.
These headers are added to every request made to the provider.
PARAMETER | DESCRIPTION |
---|---|
headers
|
The headers to add. This can be a dictionary or an iterable of key-value pairs. The iterable is preferred as it ensures that repeated headers are not lost. |
Source code in src/pact/v3/verifier.py
add_source(source: str | Path | URL, *, username: str | None = None, password: str | None = None, token: str | None = None) -> Self
¶
Adds a source to the verifier.
This will use one or more Pact files as the source of interactions to verify.
PARAMETER | DESCRIPTION |
---|---|
source
|
The source of the interactions. This may be either of the following:
If using a URL, the |
username
|
The username to use for basic HTTP authentication. This is only used when the source is a URL.
TYPE:
|
password
|
The password to use for basic HTTP authentication. This is only used when the source is a URL.
TYPE:
|
token
|
The token to use for bearer token authentication. This is only
used when the source is a URL. Note that this is mutually
exclusive with
TYPE:
|
Source code in src/pact/v3/verifier.py
add_transport(*, protocol: str, port: int | None = None, path: str | None = None, scheme: str | None = None) -> Self
¶
Add a provider transport method.
If the provider supports multiple transport methods, or non-HTTP(S) methods, this method allows these additional transport methods to be added. It can be called multiple times to add multiple transport methods.
As some transport methods may not use ports, paths or schemes, these parameters are optional.
PARAMETER | DESCRIPTION |
---|---|
protocol
|
The protocol to use. This will typically be one of:
Any other protocol will be treated as a custom protocol and will be handled by a plugin.
TYPE:
|
port
|
The provider port. If the protocol does not use ports, this parameter should be
TYPE:
|
path
|
The provider context path. For protocols which do not use paths, this parameter should be
For protocols which do use paths, this parameter should be specified to avoid any ambiguity, though if left unspecified, the root path will be used. If a non-root path is used, the path given here will be
prepended to the path in the interaction. For example, if the
path is
TYPE:
|
scheme
|
The provider scheme, if applicable to the protocol. This is typically only used for the
TYPE:
|
Source code in src/pact/v3/verifier.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
broker_source(url: str | URL, *, username: str | None = None, password: str | None = None, token: str | None = None, selector: bool = False) -> BrokerSelectorBuilder | Self
¶
broker_source(url: str | URL, *, username: str | None = None, password: str | None = None, selector: Literal[False] = False) -> Self
broker_source(url: str | URL, *, token: str | None = None, selector: Literal[False] = False) -> Self
Adds a broker source to the verifier.
PARAMETER | DESCRIPTION |
---|---|
url
|
The broker URL. TThe URL may contain a username and password for basic HTTP authentication.
TYPE:
|
username
|
The username to use for basic HTTP authentication. If the source
is a URL containing a username, this parameter must be
TYPE:
|
password
|
The password to use for basic HTTP authentication. If the source
is a URL containing a password, this parameter must be
TYPE:
|
token
|
The token to use for bearer token authentication. This is
mutually exclusive with
TYPE:
|
selector
|
Whether to return a BrokerSelectorBuilder instance.
TYPE:
|
Source code in src/pact/v3/verifier.py
disable_ssl_verification() -> Self
¶
Disable SSL verification.
Source code in src/pact/v3/verifier.py
filter(description: str | None = None, *, state: str | None = None, no_state: bool = False) -> Self
¶
Set the filter for the interactions.
This method can be used to filter interactions based on their description and state. Repeated calls to this method will replace the previous filter.
PARAMETER | DESCRIPTION |
---|---|
description
|
The interaction description. This should be a regular expression. If unspecified, no filtering will be done based on the description.
TYPE:
|
state
|
The interaction state. This should be a regular expression. If unspecified, no filtering will be done based on the state.
TYPE:
|
no_state
|
Whether to include interactions with no state.
TYPE:
|
Source code in src/pact/v3/verifier.py
filter_consumers(*filters: str) -> Self
¶
Filter the consumers.
PARAMETER | DESCRIPTION |
---|---|
filters
|
Filters to apply to the consumers.
TYPE:
|
logs_for_provider(provider: str) -> str
classmethod
¶
output(*, strip_ansi: bool = False) -> str
¶
set_coloured_output(*, enabled: bool = True) -> Self
¶
set_error_on_empty_pact(*, enabled: bool = True) -> Self
¶
Toggle error on empty pact.
If enabled, a Pact file with no interactions will cause the verifier to return an error. If disabled, a Pact file with no interactions will be ignored.
Source code in src/pact/v3/verifier.py
set_info(name: str, *, url: str | URL | None = None, scheme: str | None = None, host: str | None = None, port: int | None = None, path: str | None = None) -> Self
¶
Set the provider information.
This sets up information about the provider as well as the way it communicates with the consumer. Note that for historical reasons, a HTTP(S) transport method is always added.
For a provider which uses other protocols (such as message queues), the
add_transport
must be used.
This method can be called multiple times to add multiple transport
methods.
PARAMETER | DESCRIPTION |
---|---|
name
|
A user-friendly name for the provider.
TYPE:
|
url
|
The URL on which requests are made to the provider by Pact. It is recommended to use this parameter to set the provider URL. If the port is not explicitly set, the default port for the scheme will be used. This parameter is mutually exclusive with the individual parameters.
TYPE:
|
scheme
|
The provider scheme. This must be one of
TYPE:
|
host
|
The provider hostname or IP address. If the provider is running
on the same machine as the verifier,
TYPE:
|
port
|
The provider port. If not specified, the default port for the schema will be used.
TYPE:
|
path
|
The provider context path. If not specified, the root path will be used. If a non-root path is used, the path given here will be
prepended to the path in the interaction. For example, if the
path is
TYPE:
|
Source code in src/pact/v3/verifier.py
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
set_publish_options(version: str, url: str | None = None, branch: str | None = None, tags: list[str] | None = None) -> Self
¶
Set options used when publishing results to the Broker.
PARAMETER | DESCRIPTION |
---|---|
version
|
The provider version.
TYPE:
|
url
|
URL to the build which ran the verification.
TYPE:
|
tags
|
Collection of tags for the provider. |
branch
|
Name of the branch used for verification.
TYPE:
|
Source code in src/pact/v3/verifier.py
set_request_timeout(timeout: int) -> Self
¶
Set the request timeout.
PARAMETER | DESCRIPTION |
---|---|
timeout
|
The request timeout in milliseconds.
TYPE:
|
Source code in src/pact/v3/verifier.py
set_state(url: str | URL, *, teardown: bool = False, body: bool = False) -> Self
¶
Set the provider state URL.
The URL is used when the provider's internal state needs to be changed. For example, a consumer might have an interaction that requires a specific user to be present in the database. The provider state URL is used to change the provider's internal state to include the required user.
PARAMETER | DESCRIPTION |
---|---|
url
|
The URL to which a
TYPE:
|
teardown
|
Whether to teardown the provider state after an interaction is validated.
TYPE:
|
body
|
Whether to include the state change request in the body (
TYPE:
|
Source code in src/pact/v3/verifier.py
verify() -> Self
¶
Verify the interactions.
RETURNS | DESCRIPTION |
---|---|
Self
|
Whether the interactions were verified successfully. |