You can add Match to your project by adding it as a dependency in your Package.swift file:
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyProject",
products: [
.library(name: "MyProject", targets: ["MyProject"])
],
dependencies: [
.package(url: "https://github.com/mtynior/Match.git", .upToNextMajor(from: "0.5.0")),
],
targets: [
.target(name: "MyProject", dependencies: []),
.testTarget(name: "MyProject", dependencies: ["Match"])
]
)
Open your project in Xcode, then:
- Click File -> Add Packages,
- In the search bar type:
https://github.com/mtynior/match.git
and pressEnter
, - Once Xcode finds the library, set Dependency rule to
Up to next major version
, - Click Add Package,
- Select the Test Target (If you have multiple test targets, you can add the dependency manually from Xcode )
- Confirm selection by clicking on Add Package.
Now, when we have library integrated with our project, let's write some tests.
Imagine that we have a function that adds two numbers:
func sum(_ a: Int, _ b: Int) -> Int {
a + b
}
Not we can test our function using XCTest Framework and the Match:
import XCTest
import Match
@testable MyProject
final class SumTests: XCTestCase {
func testAddingTwoNumbers() {
// given
let expectedValue = 5
// when
let actualValue = sum(2, 3)
// then
expect(actualValue).toBeEqual(expectedValue) // Passes: "Expected: 5 is equal to received: 5"
}
}
If we implemented the sum
function correctly the test above will pass.
But let's see what will happen when we make a mistake in the sum
implementation:
func sum(_ a: Int, _ b: Int) -> Int {
a * b
}
Now, our test should fail:
import XCTest
import Match
@testable MyProject
final class SumTests: XCTestCase {
func testAddingTwoNumbers() {
// given
let expectedValue = 5
// when
let actualValue = sum(2, 3)
// then
expect(actualValue).toBeEqual(expectedValue) // Fails: "Expected: 5 to be equal to received: 6"
}
}
That's it. Now, check how to use the rest of the Matchers and start testig.
Icon is made by Freepik and was available at FlatIcon.
Match is released under the MIT license. See LICENSE for details.