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 import Verifier
# In the case of local Pact files
verifier = (
Verifier("My Provider")
.add_transport("http", url="http://localhost:8080")
.add_source("pact/to/pacts/")
)
verifier.verify()
# In the case of a Pact Broker
verifier = (
Verifier("My Provider")
.add_transport("http", url="http://localhost:8080")
.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.
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
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/verifier.py
Methods:¶
build() -> Verifier
¶
Build the Broker Selector.
| RETURNS | DESCRIPTION |
|---|---|
Verifier
|
The Verifier instance with the broker source added. |
Source code in src/pact/verifier.py
consumer_tags(*tags: str) -> Self
¶
consumer_version(*, consumer: str | None = None, tag: str | None = None, fallback_tag: str | None = None, latest: bool | None = None, deployed_or_released: Literal[True] | None = None, deployed: Literal[True] | None = None, released: Literal[True] | None = None, environment: str | None = None, main_branch: Literal[True] | None = None, branch: str | None = None, matching_branch: Literal[True] | None = None, fallback_branch: str | None = None) -> Self
¶
Add a consumer version selector.
This method allows specifying consumer version selection criteria to filter which consumer pacts are verified from the broker.
This function can be called multiple times to add multiple selectors. The resulting selectors are combined with a logical OR, meaning that pacts matching any of the selectors will be included in the verification.
| PARAMETER | DESCRIPTION |
|---|---|
consumer
|
Application name to filter the results on. Allows a selector to only be applied to a certain consumer.
TYPE:
|
tag
|
The tag name(s) of the consumer versions to get the pacts for. This field is still supported but it is recommended to use the
TYPE:
|
fallback_tag
|
The name of the tag to fallback to if the specified This is useful when the consumer and provider use matching branch names to coordinate the development of new features. This field is still supported but it is recommended to use two separate selectors - one with the main branch name and one with the feature branch name.
TYPE:
|
latest
|
Only select the latest (if false, this selects all pacts for a tag). Used in conjunction with the tag property. If a tag is specified, and latest is true, then the latest pact for each of the consumers with that tag will be returned. If a tag is specified and the latest flag is not set to true, all the pacts with the specified tag will be returned.
TYPE:
|
deployed_or_released
|
Applications that have been deployed or released. If the key is specified, can only be set to
TYPE:
|
deployed
|
Applications that have been deployed. If the key is specified, can only be set to
TYPE:
|
released
|
Applications that have been released. If the key is specified, can only be set to
TYPE:
|
environment
|
Applications in a given environment. The name of the environment containing the consumer versions for
which to return the pacts. Used to further qualify
TYPE:
|
main_branch
|
Applications with the default branch set in the broker. If the key is specified, can only be set to
TYPE:
|
branch
|
Applications with the given branch. The branch name of the consumer versions to get the pacts for. Use of this selector requires that the consumer has configured a branch name when publishing the pacts.
TYPE:
|
matching_branch
|
Applications that match the provider version branch sent during verification. If the key is specified, can only be set to
TYPE:
|
fallback_branch
|
Fallback branch if branch doesn't exist. The name of the branch to fallback to if the specified branch does not exist. Use of this property is discouraged as it may allow a pact to pass on a feature branch while breaking backwards compatibility with the main branch, which is generally not desired. It is better to use two separate consumer version selectors, one with the main branch name, and one with the feature branch name, rather than use this property.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
The builder instance for method chaining. |
Source code in src/pact/verifier.py
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 | |
consumer_versions(*versions: str) -> Self
deprecated
¶
Deprecated
Use consumer_version method with keyword arguments instead.
Set the consumer versions.
Source code in src/pact/verifier.py
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
¶
Set the provider branch.
The first time a branch is set here or through
set_publish_options, the value will be
saved and use as a default for both.
Source code in src/pact/verifier.py
Verifier(name: str, host: str | None = None)
¶
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.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name of the provider to verify. This is used to identify which interactions the provider is involved in, and then Pact will replay these interactions against the provider.
TYPE:
|
host
|
The host on which the Pact verifier is running. This is used to
communicate with the provider. If not specified, the default
value is
TYPE:
|
Source code in src/pact/verifier.py
Attributes¶
logs: str
property
¶
Get the logs.
results: dict[str, Any]
property
¶
Get the results.
Methods:¶
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/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. The value can be: - a dictionary of header key-value pairs - an iterable of (key, value) tuples |
Source code in src/pact/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:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the source scheme is invalid. |
Source code in src/pact/verifier.py
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 | |
add_transport(*, url: str | URL | None = None, protocol: str | None = None, 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. Note that while optional, these may still be
used during testing as Pact uses HTTP(S) to communicate with the
provider. For example, if you are implementing your own message
verification, it needs to be exposed over HTTP and the port and path
arguments are used for this testing communication.
| PARAMETER | DESCRIPTION |
|---|---|
url
|
A convenient way to set the provider transport. This option is mutually exclusive with the other options.
TYPE:
|
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. If
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:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If mutually exclusive parameters are provided, or required parameters are missing, or host/protocol mismatches. |
Source code in src/pact/verifier.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
broker_source(url: str | URL | Unset | None = UNSET, *, username: str | Unset | None = UNSET, password: str | Unset | None = UNSET, token: str | Unset | None = UNSET, selector: bool = False, use_env: bool = True) -> BrokerSelectorBuilder | Self
¶
broker_source(
url: str | URL | Unset = UNSET,
*,
username: str | Unset = UNSET,
password: str | Unset = UNSET,
selector: Literal[False] = False,
use_env: bool = True,
) -> Self
broker_source(
url: str | URL | Unset | None = UNSET,
*,
token: str | Unset | None = UNSET,
selector: Literal[False] = False,
use_env: bool = True,
) -> Self
Adds a broker source to the verifier.
If any of the values are None, the value will be read from the
environment variables unless the use_env parameter is set to False.
The known variables are:
PACT_BROKER_BASE_URLfor theurlparameter.PACT_BROKER_USERNAMEfor theusernameparameter.PACT_BROKER_PASSWORDfor thepasswordparameter.PACT_BROKER_TOKENfor thetokenparameter.
By default, or if selector=False, this function returns the verifier
instance to allow for method chaining. If selector=True is given, this
function returns a BrokerSelectorBuilder
instance which allows for further configuration of the broker source in
a fluent interface. The build() call is
then used to finalise the broker source and return the verifier instance
for further configuration.
| PARAMETER | DESCRIPTION |
|---|---|
url
|
The broker URL. The URL may contain a username and password for basic HTTP authentication. |
username
|
The username to use for basic HTTP authentication. If the source
is a URL containing a username, this parameter must be |
password
|
The password to use for basic HTTP authentication. If the source
is a URL containing a password, this parameter must be |
token
|
The token to use for bearer token authentication. This is
mutually exclusive with |
selector
|
Whether to return a
BrokerSelectorBuilder instance. The
builder instance allows for further configuration of the broker
source and must be finalised with a call to
TYPE:
|
use_env
|
Whether to read missing values from the environment variables.
This is
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If mutually exclusive authentication parameters are provided. |
Source code in src/pact/verifier.py
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 | |
disable_ssl_verification() -> Self
¶
Disable SSL verification.
Source code in src/pact/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/verifier.py
filter_consumers(*filters: str) -> Self
¶
Filter the consumers.
| PARAMETER | DESCRIPTION |
|---|---|
filters
|
Filters to apply to the consumers.
TYPE:
|
follow_redirects(follow: bool) -> Self
¶
Set whether redirects should be followed.
By default, the Pact verifier does follow redirects, testing the final non-redirect response (mimicking the default behaviour of most HTTP clients).
In some cases, it may be desirable to test the redirect response itself, in which case this method can be used to disable following redirects.
Source code in src/pact/verifier.py
logs_for_provider(provider: str) -> str
classmethod
¶
message_handler(handler: Callable[..., Message] | dict[str, Callable[..., Message] | Message | bytes]) -> Self
¶
Set the message handler.
This method sets a custom message handler for the verifier. The handler can be called to produce a specific message to send to the provider.
This can be provided in one of two ways:
-
A fully fledged function that will be called for all messages. This is the most powerful option as it allows for full control over the message generation. The function's signature must be compatible with the
MessageProducerArgstype. -
A dictionary mapping message names to either (a) producer functions, (b)
Messagedictionaries, or © raw bytes. If using a producer function, it must be compatible with theMessageProducerArgstype.
Implementation¶
There are a large number of ways to send messages, and the specifics of the transport methods are not specifically relevant to Pact. As such, Pact abstracts the transport layer away and uses a lightweight HTTP server to handle messages.
Pact Python is capable of setting up this server and handling the
messages internally using user-provided handlers. It is possible to use
your own HTTP server to handle messages by using the add_transport
method. It is not possible to use both this method and add_transport
to handle messages.
| PARAMETER | DESCRIPTION |
|---|---|
handler
|
The message handler. This should be a callable or a dictionary mapping message names to callables, Message dicts, or bytes.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the handler or its values are invalid. |
KeyError
|
If a message is requested which is not present in the dictionary. As the handler is called during verification, this is raised within the message relay server and surfaces as a failed interaction. |
Source code in src/pact/verifier.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | |
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/verifier.py
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. The first time a branch is set here or through
TYPE:
|
Source code in src/pact/verifier.py
set_request_timeout(timeout: int) -> Self
¶
Set the request timeout.
| PARAMETER | DESCRIPTION |
|---|---|
timeout
|
The request timeout in milliseconds.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the timeout is negative. |
Source code in src/pact/verifier.py
state_handler(handler: Callable[..., None] | Mapping[str, Callable[..., None]] | StateHandlerUrl, *, teardown: bool = False, body: bool | None = None) -> Self
¶
state_handler(
handler: Callable[..., None],
*,
teardown: bool = False,
body: None = None,
) -> Self
Set the state handler.
In many interactions, the consumer will assume 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").
The state handler is responsible for changing the provider's internal state to match the expected state before the interaction is replayed.
This can be done in one of three ways:
- By providing a single function that will be called for all state changes.
- By providing a mapping of state names to functions.
- By providing the URL endpoint to which the request should be made.
The last option is more complicated as it requires the provider to be able to handle the state change requests. The first two options handle this internally and are the preferred options if the provider is written in Python.
The function signature must be compatible with the
StateHandlerArgs. If the function has
additional arguments, these must either have default values, or be
filled by using the partial function.
Pact also uses a special state denoted with the empty string "". This
is used as a generic test setup/teardown handler. This key is optional
in dictionaries, but other implementation should ensure they can handle
(or safely ignore) this state name.
| PARAMETER | DESCRIPTION |
|---|---|
handler
|
The handler for the state changes. This can be one of the following:
See above for more information on the function signature.
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:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the handler/body combination is invalid. |
TypeError
|
If the handler type is invalid. |
Source code in src/pact/verifier.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
verify() -> Self
¶
Verify the interactions.
| RETURNS | DESCRIPTION |
|---|---|
Self
|
Whether the interactions were verified successfully. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If no transports have been set. |