Skip to content

JPrendy/swift-ui-snapshot-application

Repository files navigation

swift ui snapshot application

Description

A Swift Application that uses SwiftUI and uses the library swift snapshot testing to take snapshots

Contents

Setup Steps

To use the test tool Swift-snapshot-tesing, we first need to import the test dependency, the easiest way by using Swift Package Manager.

Go to File then Swift Packages, then select Add Package Dependencies then add the Github url https://github.com/pointfreeco/swift-snapshot-testing to the LandMarksUITests.

To add an environment variable to the scheme LandMarksUITests. See the following image

Then you want to go to LandMarksUITests.swift and add the following to the setUp, this will make it easy to determine when we want and don't want to record snapshot.

override func setUp(){
    if ProcessInfo.processInfo.environment["SNAPSHOT_RECORD_MODE"] == "true" {
    SnapshotTesting.isRecording = true
    }
}

An example of a snapshot test that uses Swift-snapshot-testing is the one below, we define what device we want and what the end name of the image should be, so the image will be called testTableViewController.iphone-8.png when we record it.

func testTableViewController() {
    class TableViewController: UITableViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
    }
    let tableViewController = TableViewController()
    assertSnapshot(matching: tableViewController, as: .image(on: .iPhone8), named: "iphone-8")
}

An example of a snapshot test assert than you can use to test different font sizes

assertSnapshot(matching: tableViewController, as: .image(on: .iPhone8, traits: .init(preferredContentSizeCategory: .extraLarge)), named: "iphone-8")

How to run the project locally

To run the snapshot tests locally, run the following.

bundle exec fastlane run_snapshots

To record new snapshots locally, run the following.

bundle exec fastlane record_snapshots

Tools

Linter: we use the following linter link.

Uploading Artifacts: we use the following way to upload Artifacts, they allow you to persist data like test results after a job has completed, see the following documentation link.

Creating images/icons: we use Figma to create images and icon. Figma makes it very easy to create designs in many different formats.

Creating a Mock Server: we use a mock server with Postman to quickly test apis, to see how to create a mock server, see the following video link.

Mobile Specific Tools:

Fastlane: Fastlane allows us to automate our development and release process link.

App Center: App Center is used to distribute an app, making it very easy to test on a physical device by using a fastlane plugin link.

Proxyman: we use Proxyman to view HTTP/HTTPS requests as they happen, it is easier to debug network connections on mobile on Proxyman where we can test and mock specific network responses, see the following documentation link.

Update Dependencies

Npm: How to update a npm package.

Gemfile: How to update a Gemfile package.

Releases

How to manage releases in a repository link.

Helpful resources

The following links to a Swift snapshot library called swift snapshot testing that you can use to take snapshots.

The following links to a good tutorial on how to use swift snapshot testing.

The following links to a guide on how you can take snapshots of SwiftUI views.

The following links to a guide by Apple on how to create views in SwiftUI, it's a very handy guide.

The following links to a video tutorial for SwiftUI Basics for Beginners.

The following links to a guide on how to add a bar button to a navigation bar.

N.B. The following links to a guide on how view controllers works including how to update a view and adding a child view to a view controller.

The following links to a test from swift snapshot testing called testTableViewController.

The following links to a test from swift snapshot testing called testTraitsEmbeddedInTabNavigation.

The following links to a test from swift snapshot testing that updates the size of the text, good for tests that test different font sizes.

The following links to how you set up environment variable in iOS, this setup is very helpful for testing.

The following links to a fix to the following error The scheme 'LandmarksUITests' has nothing configured to build for Running and has no executable specified to Run. Edit the scheme to configure the Run action.

The following links to how use xcode_select, which change the xcode-path to use. Useful for beta versions of Xcode. Good to use instead of xcversion, as it has been deprecated by Fastlane and will show an error message in the logs.