Tidy up your XCTests with swift

Related tags

Color TestCleaner
Overview

TestCleaner

It makes your tests cleaner.

Here is an existing XCTest that confirms that a Version type’s ExpressibleByStringLiteral is working by comparing the results to known good values:

// Before
func testExpressibleByStringLiteral() {
  XCTAssertEqual("1", Version(major: 1)),
  XCTAssertEqual("1.0", Version(major: 1)),
  XCTAssertEqual("1.0.0", Version(major: 1)),
  XCTAssertEqual("1.2", Version(major: 1, minor: 2)),
  XCTAssertEqual("4.5.6", Version(major: 4, minor: 5, bugfix: 6)),
  XCTAssertEqual("10.1.88", Version(major: 10, minor: 1, bugfix: 88)),
}

Using TestCleaner, we can clean this up a little without sacrificing Xcode’s ability to highlight lines containing failing tests. We also recommend using a local typealias to help reduce line noise:

// After
func testExpressibleByStringLiteral() {
  typealias V = Version
  assertEqual(testCases: [
    Pair("1", V(major: 1)),
    Pair("1.0", V(major: 1)),
    Pair("1.0.0", V(major: 1)),
    Pair("1.2", V(major: 1, minor: 2)),
    Pair("4.5.6", V(major: 4, minor: 5, bugfix: 6)),
    Pair("10.1.88", V(major: 10, minor: 1, bugfix: 88)),
  ])
}

Now, the assert operation (“equal”) needs to be written in just one place, making the block of tests less error-prone and the intent clearer. It also reduces line length, although the typealias is helping there.

Focus or Skip Tests

Borrowing syntax from Quick, you can focus any test or tests by adding an f to the beginning, and only those tests will execute on the next run, allowing you to debug individual cases without having to haphazardly comment and uncomment lines:

func testExpressibleByStringLiteral() {
  typealias V = Version
  assertEqual(testCases: [
    Pair("1", V(major: 1)),
    fPair("1.0", V(major: 1)), // Only this test will run.
    Pair("1.0.0", V(major: 1)),
  ]
}

You can also skip test cases by prepending them with x:

func testExpressibleByStringLiteral() {
  typealias V = Version
  assertEqual(testCases: [
    Pair("1", V(major: 1)),
    xPair("1.0", V(major: 1)), // These last two tests will be ignored.
    xPair("1.0.0", V(major: 1)),
  ]
}

These can be combined in a single test for added flexibility: use xPair to skip some tests to start with, then focus in on just one to debug it with fPair.

All Available Comparators

TestCleaner mirrors the full range of XCTAssert functions available:

  • Boolean (true/false)
  • LessThan <
  • GreaterThan >
  • LessThanOrEqual <=
  • GreaterThanOrEqual >=
  • Equal ==
  • Equal (with floating-point accuracy) ==
  • NotEqual !=
  • NotEqual (with floating-point accuracy) !=

Custom Assertions

If you have a custom assertion function, you can use the assertCustom function to use it with TestCleaner. Just make sure your custom assertion takes file: StaticString, line: UInt parameters, and forward the ones that the tests closure in assertCustom passes to you.

assertCustom(
  testCases: [
    Pair(someLeftValue, someRightValue),
    Pair(anotherLeftValue, anotherRightValue),
  ],
  tests: { pair, file, line in
    myCustomAssertion(
      pair.left, pair.right,
      file: file, line: line // <-- ⚠️ this is important!
    )
    try youCanAlsoThrowErrorsInHere() // They will also get attributed to the correct line.
  }
)

When To Use TestCleaner

This tool is ideal for tests of pure transformations, where a certain input will always produce the same output. Examples include: parsing, mapping, converting, and calculating. It is not intended to be used for integration-style tests, where each assertion is preceded by many lines of setup.

Further Reading

TestCleaner was inspired by a blog post which was in turn inspired by a conversation with Brian King.

You might also like...
ImagePalette - Swift/iOS port of Android's Palette

ImagePalette - Swift/iOS port of Android's Palette

Google Material Color Palette in Swift

Google Material Color in Swift Defined Google Material Color value ready to use in Swift refer to Google Material Design in Style/Color section ###How

A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy!

Colors A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy! Note: Colors master requires Xcode 7.3

A Lightweight But Powerful Color Kit (Swift)
A Lightweight But Powerful Color Kit (Swift)

BCColor A lightweight but powerful color kit (Swift) Features Pick Colors From Image Generate Monochrome Image Support Hex Color Style Lighten / Darke

Aesthetic color-scheme generation written in Swift
Aesthetic color-scheme generation written in Swift

Lorikeet Lightweight framework for generating visually aesthetic color-schemes in Swift Requirements UIKit Features What can Lorikeet do for you Calcu

A pure Swift library that allows you to easily convert SwiftUI Colors to Hex String and vice versa.
A pure Swift library that allows you to easily convert SwiftUI Colors to Hex String and vice versa.

iOS · macOS · watchOS · tvOS A pure Swift library that allows you to easily convert SwiftUI Colors to Hex String and vice versa. There is also support

SwiftOpenColor - Swift port of OpenColor

Using Swift Package Manager, add it as a Swift Package in Xcode 11.0 or later

Powerful animated gradientView in swift 🌈
Powerful animated gradientView in swift 🌈

FancyGradient is a UIView subclass which let's you animate gradients in your iOS app. It is purely written in Swift. Quickstart Static gradient let fa

An Adobe .ase (Adobe Swatch Exchange File), .aco (Photoshop swatch file) reader/writer package for Swift (macOS, iOS, tvOS, macCatalyst)

ColorPaletteCodable A palette reader/editor/writer package for iOS, macOS, watchOS and tvOS, supporting the following formats Adobe Swatch Exchange (.

Releases(0.1.0)
Owner
Zev Eisenberg
I was raised by clowns, but I ran away to join the circuits.
Zev Eisenberg
A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.

Installation Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how

Ben Gordon 3.1k Dec 28, 2022
ChromaColorPicker - An intuitive HSB color picker built in Swift

An intuitive HSB color picker built in Swift. Supports multiple selection handles and is customizable to your needs.

Jonathan Cardasis 536 Dec 29, 2022
Yet another extension to manipulate colors easily in Swift and SwiftUI

DynamicColor provides powerful methods to manipulate colors in an easy way in Swift and SwiftUI. Requirements • Usage • Installation • Contribution •

Yannick Loriot 2.8k Dec 30, 2022
Gradients 🌔 A curated collection of 180 splendid gradients made in swift

Gradients ?? A curated collection of 180 splendid gradients made in swift 180 splendid Gradients inspired by itmeo/webgradients View all the gradients

Gradients 775 Jan 1, 2023
PrettyColors is a Swift library for styling and coloring text in the Terminal.

PrettyColors is a Swift library for styling and coloring text in the Terminal. The library outputs ANSI escape codes and conforms to ECMA Standard 48.

J.D. Healy 171 Aug 13, 2022
HEX color handling as an extension for UIColor. Written in Swift.

SwiftHEXColors HEX color handling as an extension for UIColor. Written in Swift.

Thi Doãn 692 Dec 22, 2022
Color framework for Swift & Objective-C (Gradient colors, hexcode support, colors from images & more).

Swift 3 To use the Swift 3 version, add this to your Podfile (until 2.2 or higher is released): pod 'ChameleonFramework/Swift', :git => 'https://githu

Vicc Alexander 12.5k Dec 27, 2022
An attractive color generator for Swift. Ported from randomColor.js.

Random Color Swift Inspired by David Merfield's randomColor.js. It is a ported version to Swift. You can use the library to generate attractive random

Wei Wang 624 Jan 3, 2023
Flat UI color palette helpers written in Swift.

FlatUIColors (swift) install Make sure you have the latest version of CocoaPods (gem install cocoapods) that has Swift support. At the time of this wr

Bryn Bellomy 172 Dec 6, 2022
Conical (angular) gradient for iOS written in Swift

AEConicalGradient Conical (angular) gradient in Swift I hope that somebody will find this useful. And nice. Usage AEConicalGradient is a minion which

Marko Tadić 82 Dec 27, 2022