Skip to content

How-To: Verify multiple pacts in a directory or from multiple file path locations #362

Closed
@YOU54F

Description

@YOU54F

We got asked the question in slack, as to whether you could verify pacts in a directory or from multiple locations.

The function currently expects a comma separated string of file paths, so passing a directory as the first argument will fail.

Here are a couple of examples. feel free to add your own

Read by Directory

This commit shows a working example (utilising pact-python 2.0.0)

88918cf

    def list_full_paths(directory):
        return [os.path.join(directory, file) for file in os.listdir(directory)]

    output, _ = verifier.verify_pacts(
        ','.join(map(str, list_full_paths("../pacts"))),
        verbose=False,
        provider_states_setup_url="{}/_pact/provider_states".format(PROVIDER_URL),
    )

Might not be an ideal solution, but if it gets readers out of a bind.

Read from multiple locations

Solution provided by Ivan Mikhalka over in Slack, thanks 👍🏾

            pact_files = []
            if not os.environ.get("CONSUMER_1_CONTRACT_PATH") is None:
                pact_files.append("{}".format(os.environ["CONSUMER_1_CONTRACT_PATH"]))
            if not os.environ.get("CONSUMER_2_CONTRACT_PATH") is None:
                pact_files.append("{}".format(os.environ["CONSUMER_2_CONTRACT_PATH"]))
            success, logs = verifier.verify_pacts(*pact_files)
export CONSUMER_1_CONTRACT=<path/to/contract1> && export CONSUMER_2_CONTRACT=<path/to/contract2> && pytest provider_test.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions