Example Pact Swift project using Carthage
October 29, 2020 ยท View on GitHub
See the Pact Swift library for more details. PactConsumerSwift library
Installation with Carthage
Install Pact Mock Service
Install the pact-ruby-standalone as per installation instructions.
Add the PactConsumerSwift library to your project
- Add
github "DiUS/pact-consumer-swiftto your Cartfile - Follow the Carthage guidelines for building and adding a framework with Carthage: Carthage
- Add the
PactConsumerSwift.frameworkto your test target - For iOS projects, you must add a run script to copy the frameworks to the correct location. See the Carthage documentation for more info.
Setup your Test Target to run the pact server before the tests are run
Modify the Test Target's scheme to add scripts to start and stop the pact server when tests are run.
-
From the menu
Product->Scheme->Edit Scheme -
Under Test, Pre-actions add a Run Script Action Add a Run Script Action with the following NB: the PATH variable should be set to the location of the pact-mock-service binary - you can find the path using
which pact-mock-servicePATH=/path/to/pact-mock-service/binary:$PATH "$SRCROOT"/Carthage/Checkouts/pact-consumer-swift/scripts/start_server.sh- Make sure you select your project under
Provide the build settings from, otherwise SRCROOT will not be set which the scripts depend on
- Make sure you select your project under

-
Under Test, Post-actions add a Run Script Action to stop the pact service.
PATH=/path/to/pact-mock-service/binary:$PATH "$SRCROOT"/Carthage/Checkouts/pact-consumer-swift/scripts/stop_server.sh- Make sure you select your project under
Provide the build settings from, otherwise SRCROOT will not be set which the scripts depend on
- Make sure you select your project under
Writing tests
See PactSwifExampleTests.swift for examples of writing Pact tests in Swift. For Objective-C see Pact ObjectiveC Example