GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - approvals/ApprovalTests.Swift: ApprovalTests for Swift, a powerful alternative to assertions

BranchesTags
Open more actions menu

Latest commit

 

History

822 Commits
822 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
ApprovalTests.Swift.xcodeproj
ApprovalTests.Swift.xcodeproj
 
 
ApprovalTests.Swift
ApprovalTests.Swift
 
 
ApprovalTests_SwiftTests
ApprovalTests_SwiftTests
 
 
ApprovalTests_iOS
ApprovalTests_iOS
 
 
ApprovalTests_iOSTests
ApprovalTests_iOSTests
 
 
SamplePhoneApp.xcworkspace
SamplePhoneApp.xcworkspace
 
 
SamplePhoneApp
SamplePhoneApp
 
 
docs
docs
 
 
.gitignore
.gitignore
 
 
.swift-version
.swift-version
 
 
.swiftformat
.swiftformat
 
 
.swiftlint.yml
.swiftlint.yml
 
 
ApprovalTests_Swift.podspec
ApprovalTests_Swift.podspec
 
 
CHANGELOG.md
CHANGELOG.md
 
 
Cartfile.project
Cartfile.project
 
 
FirstTestApproved.png
FirstTestApproved.png
 
 
FirstTestResult.png
FirstTestResult.png
 
 
LICENSE
LICENSE
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 
ToDo.md
ToDo.md
 
 
XcodeWarnings.xcconfig
XcodeWarnings.xcconfig
 
 
iOSApprovalsWatcher.py
iOSApprovalsWatcher.py
 
 
mdsnippets.json
mdsnippets.json
 
 
merge_dependabot.sh
merge_dependabot.sh
 
 
run_tests_ios.sh
run_tests_ios.sh
 
 
run_tests_mac.sh
run_tests_mac.sh
 
 
runtests.sh
runtests.sh
 
 

Repository files navigation

ApprovalTests

Build Status Swift Package Index Swift Version Compatibility Swift Package Index Platform Compatibility Carthage compatible CocoaPods Version

Approval Tests are an alternative to assertions. You’ll find them useful for testing objects with complex values (such as long strings), lots of properties, or collections of objects.

ApprovalTests.Swift is compatible with the XCTest testing framework.

Contents

  • My First Approval Test
  • Getting Started
  • Predefined Verifiers
  • How to Use with iOS
  • Which File Artifacts to Exclude from Source Control
  • Other Ways to Get It
    • Swift Package Manager
    • Carthage
    • CocoaPods
  • For More Information
    • Questions?
    • Documentation
    • Video Tutorials
    • Podcasts

My First Approval Test

We'll start by writing a simple unit test to verify a list of names. But instead of using XCTest's XCTAssertEqual function, we'll use Approvals.verifyAll:

class SampleArrayTests: XCTestCase {
    func testList() throws {
        var names = ["Llewellyn", "James", "Dan", "Jason", "Katrina"]
        names.sort()
        try Approvals.verifyAll(names, label: "")
    }

snippet source | anchor

The verifyAll() function performs a test assertion for a list of items. Unlike a normal assertion, it doesn’t specify an expected list. Instead, this will produce a “received” file matching the name of your test suite and test case. In this example, it will write a file SampleArrayTests.testList.received.txt:

[0] = Dan
[1] = James
[2] = Jason
[3] = Katrina
[4] = Llewellyn

snippet source | anchor

It also opens two files in a diff editor—the “received” file, and the “approved” file.

Results shown in diff editor

To approve the results, tell the diff editor to apply changes from the left side to the right side:

Results copied from received to approved

Most of the time, you’ll use one of the supported diff tools to examine and approve the result. If you don’t have any of these diff tools, you can rename the received file to SampleArrayTests.testList.approved.txt and the test will now pass.

Getting Started

The best way to get started is download and open one of the starter projects:

  • macOS
  • iOS

These are standard projects and can be imported into any editor or IDE.
They also all have CI with Github actions.

They come ready with:

  • A Swift package definition adding ApprovalTests.Swift to the test target
  • A suitable .gitignore to exclude approval artifacts
  • A GitHub action to build and run tests
  • A GitHub build status badge

Predefined Verifiers

ApprovalTests.Swift comes with useful verifiers:

  • Approvals.verify — verify object or dictionary
  • Approvals.verifyAll — verify array of items (or use array as inputs)
  • Approvals.verifyAsJSON — verify Encodable object converted to JSON
  • Approvals.verifyQuery — verify query, also showing query results on failure
  • Approvals.verifySequence — verify sequence of changing values

How to Use with iOS

ApprovalTests.Swift runs out-of-the-box for macOS tests. But for iOS tests, you need a separate process running on your Mac to watch for diffs. Run iOSApprovalsWatcher.py from your command line, giving it the path to your test directory.

Which File Artifacts to Exclude from Source Control

You must add any “approved” files to your source control system. But “received” files can change with any run and should be ignored. For Git, add this to your .gitignore:

*.received.*

If you have iOS tests, you should also add command.sh to your .gitignore. (They are scripts written by the iOS side for the file monitor to execute from the macOS side.) So for iOS testing, make sure to exclude:

*.received.*
command.sh

Other Ways to Get It

Swift Package Manager

See an example package manifest here

Get the following dependency:

dependencies: [
    .package(
        url: "https://github.com/approvals/ApprovalTests.Swift",
        branch: "master"
    ),
],

anchor

Then add it to your test target:

.testTarget(
    name: "ApprovalTests.Swift.StarterProject.MacOSTests",
    dependencies: [
        "ApprovalTests.Swift.StarterProject.MacOS",
        "ApprovalTests.Swift",
    ],

anchor

Carthage

Add the following to your Cartfile:

github "approvals/ApprovalTests.Swift" ~> 2.0

Then drag the the built framework from the appropriate Carthage/Build directory into your project, but with “Copy items into destination group’s folder” disabled.

CocoaPods

If you want to add ApprovalTests.Swift using Cocoapods then add the following dependency to your Podfile. Most people will want ApprovalTests.Swift in their test targets, and not include any pods from their main targets:

target 'MyTests' do
  inherit! :search_paths
  use_frameworks!
  pod 'ApprovalTests_Swift', '~> 2.0'
end

For More Information

Questions?

Ask Llewellyn Falco @LlewellynFalco on Twitter, or Jon Reid on BlueSky or Mastodon.

Documentation

Find some extra documentation here.

Video Tutorials

  • Getting Started with ApprovalTests.Swift
  • How to Verify Objects (and Simplify TDD)
  • Verify Arrays and See Simple, Clear Diffs
  • Write Parameterized Tests by Transforming Sequences
  • Wrangle Legacy Code with Combination Approvals

You can also watch a series of short videos about using ApprovalTests in .Net on YouTube.

Podcasts

Prefer learning by listening? Then you might enjoy the following podcasts:

  • This Agile Life
  • Hanselminutes
  • Herding Code
  • The Watir Podcast

About

ApprovalTests for Swift, a powerful alternative to assertions

approvaltests.com

Topics

approval-testapproval-testingsnapshot-testingtestingxctest

Resources

Readme
Apache-2.0 license
Activity
Custom properties

Stars

93 stars

Watchers

7 watching

Forks

14 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.