TestKit has been upgraded to a full solution for implementing Behavior-Driven Development (BDD) in Swift iOS apps.

Related tags

Testing TestKit
Overview

Overview

TestKit has been upgraded to a full solution for implementing Behavior-Driven Development (BDD) in Swift iOS apps.

In a nutshell, BDD means that anyone on your team (not just developers or technical people) can write a plain-English specification for a certain feature or behavior in the app, and that specification will be executed and verified as an automated test by the TestKit framework.

Example

A developer receives the following requirement for refund behavior:

Scenario: Refunds return money to user’s account Given I am a user named Mary Smith with a $20.00 balance on my account And I have previously purchased a $15.00 book When I navigate to my purchases page And request a refund on the book And the refund is approved Then I have a $35.00 balance on my account

This requirement can be copied and pasted into a .feature file inside the application’s test target. When the test target runs, TestKit will read and parse the scenario, and execute the steps, attempting to validate them. Developers create “hooks” or “step handlers” which connect the statements in the scenario to actual functionality in the app. If there are step handlers missing for the scenario, the test will fail. If the step handlers exist, but the requirements in the scenario are not validated by the test results, the test will fail.

This gives your team an easy path to some of the basic principles of test-driven development. Specifically, the test is written and will run and fail before any coding starts. When the code is written to meet the requirement, the test will pass and the task is done.

Step handlers are also written inside of your test target, not in your production code. Step handlers that use XCUITesting to simulation user input with the app like tapping buttons, swiping through a table, or navigating to different screens are added in the application’s UI test target. Step handlers that read and set data inside the application directly, not through UI interactions, are added in the application normal (unit) test target.

An example of a step handler in TestKit for the above scenario could look this:

.*) with a $(?.*) balance on my account") { let userName: String = $0["userName"] let balance: Float = ($0["dollarAmount"] as NSString).floatValue let user = User(name: userName) user.currentBalance = balance User.setCurrentUser(to: user) }">
TestKit.given("I am a user named (?.*) with a $(?.*) balance on my account") {
   let userName: String = $0["userName"]
   let balance: Float = ($0["dollarAmount"] as NSString).floatValue
   let user = User(name: userName)
   user.currentBalance = balance
   User.setCurrentUser(to: user) 
}

This step handler would be defined in the unit test target for the application, and specifies a pattern to match in a scenario step, as well as code that runs when the pattern matches the currently executing step. As you can see, it’s easy to capture dynamic values in the step description (like the user’s name and their balance) as named variables, and then reference those dynamic values in the code that runs for the step.

The step handler for the step “When I navigate to my purchases page” would probably involve UI interaction and so would be defined in the UI test target, and might look something like this:

TestKit.when("I navigate to my purchases page") {
  let app = XCUIApplication()
  app.buttons["My Account"].tap()
  app.buttons["Purchases"].tap()
}

More Examples

For more examples of how to write scenarios and step handlers, open and examine and run the code in the TestKitExampleProject that is part of this repo.

Installation

Add TestKit as a dependency for your test target only (not the actual app or framework target) using Swift Package Manager.

Adding a Test Case

Reference

Gherkin Syntax

Step Handler Pattern Matching Syntax

Writing Unit Tests With TestKit

You may know that TestKit started as a unit testing framework which separated out test inputs and outputs into an external JSON file which could then be modified to add new test cases without writing additional code.

The latest version of TestKit still adheres to this same principle and the same goals, but now uses a broad standard (Gherkin) instead of a custom JSON schema for defining the test inputs and outputs. In fact, not only are the .feature files of the new version of TestKit much more concise and easily readable than the old .testkit files, but the process of adding step handlers and the code needed to validate unit test output in Swift has also been simplified.

The TestKitExample project includes an example of unit testing a function with multiple inputs and expected outputs, which can be used as a reference. The advantages of writing unit tests in this way, instead of completely in code are:

  1. Unit tests can be described by tech leads or fellow developers as scenarios and immediately checked by TestKit. Developers often aren’t sure what unit tests to write, and TestKit allows the actual requirements for a function which describe happy path cases, edge cases, failure cases, etc. to be provided by QA, developers or tech leads and run directly as the tests.

  2. New test cases, inputs and expected outputs can be added in plain English at any time without needing to touch actual code.

  3. Because Gherkin is a standard already used by testing tools on most platforms (like Cucumber, which is available for many platforms and languages), unit tests written with TestKit can be shared across platforms and used to ensure that the same logic is implemented for any given function on Web, Android, iOS, etc.

  4. Writing unit tests in Gherkin with TestKit documents the expectations and requirements for your functions and business logic in plain English, is always up-to-date by definition (because out of date documentation == a failing test), and clearly described edge cases and examples of how the code is expected to work.

You might also like...
Kfm-ios-test - Test online for iOS Developer position in Kimia Farma or PT. Buana Varia Komputama
Kfm-ios-test - Test online for iOS Developer position in Kimia Farma or PT. Buana Varia Komputama

kfm-ios-test Kimia Farma Mobile iOS Test Test online for iOS Developer position

PinpointKit is an open-source iOS library in Swift that lets your testers and users send feedback with annotated screenshots using a simple gesture.
PinpointKit is an open-source iOS library in Swift that lets your testers and users send feedback with annotated screenshots using a simple gesture.

PinpointKit is an open-source iOS library in Swift that lets your testers and users send feedback with annotated screenshots using a simple gesture. F

Snapshot testing tool for iOS and tvOS
Snapshot testing tool for iOS and tvOS

SnapshotTest is a simple view testing tool written completely in Swift to aid with development for Apple platforms. It's like unit testing for views.

ShortWebCore - This iOS library lets you run automations on Web Views.

This iOS library lets you run automations on Web Views. Example (Optional) Declare class conforming to AutomationRunnerDelegate: import S

Library for unifying the approach to network mocking in iOS unit- & UI-tests.

TinkoffMockStrapping Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installati

test ios UnitTest and UITest

github_actions Bundlerの導入 fastlaneやiOSパッケージマネージャであるCocoaPodsはRubyのライブラリ 開発チームで使用するバージョンを揃えるためにBundlerを導入する bundlerのインストール gem install bundler Gemfile

Fizz Buzz stub for interviewing iOS candidates

FizzBuzz Write a program that accepts an integer =1 and returns a list of strings For numbers divisible by 3, return “Fizz” For numbers divisible by

iOS Test Hasitha

Welcome to the Bidone iOS Coding Test! iOS Create a simple application with the list of orders from viewModel and display them in a list. When a user

iOS Test for Openbank by David Moreno Lora
iOS Test for Openbank by David Moreno Lora

MarvelMobileTest-iOS iOS Test for Openbank by David Moreno Lora Installation Clone the project and install the dependencies using pod install Once th

Comments
  • Enhancements and small bug fix

    Enhancements and small bug fix

    • Support and validate test closures that produce an array of TestableOutput
    • TestKitDictionary now converts any child dictionaries to TestKitDictionaries as well, and tracks child keys used
    • Fixed bug where console output would sometimes say input was both verified and failed.
    opened by daniel-hall 0
  • Code cleanup, added error expectations

    Code cleanup, added error expectations

    Refactored code for better reusability across multiple variations of the run() method by using a state monad pattern. Other refactors for clarity and to expose a way to get test failures bubbled up to the XCTest file to call XCTFail for convenience of seeing the failure message right in Xcode for that test. Added the final requirement for version 1, which is expected errors for certain inputs.

    opened by daniel-hall 0
Releases(v1.0)
  • v1.0(Apr 20, 2021)

    Since TestKit is being used successfully in multiple projects, bumping to v1.0. Updated to be a Swift Package and added the ability to pass in a closure that runs before each Scenario / Example to do necessary setup

    Source code(tar.gz)
    Source code(zip)
  • v0.6(Jan 24, 2018)

    • Updates TestKit and the TestKitExample project to Swift 4
    • Fixed a Gherkin parsing bug
    • Added option to exclude certain tags from a test run, in addition to the existing option to limit test execution to specific tags
    • Added the ability for features in the target app to register for and handle notifications from the UI test target, for setting up preconditions, etc.
    • Much improved method of conditionally injecting TestKit and the unit test bundle into the target app (see TestKitExample project)
    • BREAKING CHANGE: All registration of step handlers, and interaction with TestKit API is now exposed via the TestKitFeature class (and its subclasses). These feature classes can no longer call static methods on the TestKit type itself for registering step handlers, etc.
    Source code(tar.gz)
    Source code(zip)
  • v0.5(Nov 28, 2016)

    • Complete API for testing and validating functions with optional and non-optional returns, and for testing and validating errors that may be thrown
    • Example project with example tests
    • Complete documentation in README.md
    Source code(tar.gz)
    Source code(zip)
Owner
Daniel Hall
Working on great user experiences and the frameworks & tools to build them faster
Daniel Hall
BDD Framework and test runner for Swift projects and playgrounds

Spectre Special Executive for Command-line Test Running and Execution. A behavior-driven development (BDD) framework and test runner for Swift project

Kyle Fuller 392 Jan 1, 2023
BDD-style framework for Swift

Sleipnir Sleipnir is a BDD-style framework for Swift. Sleipnir is highly inspired by Cedar. Also In Norse mythology, Sleipnir is Odin's steed, is the

Railsware 846 Nov 22, 2022
A light-weight TDD / BDD framework for Objective-C & Cocoa

Specta A light-weight TDD / BDD framework for Objective-C. FEATURES An Objective-C RSpec-like BDD DSL Quick and easy set up Built on top of XCTest Exc

Specta / Expecta 2.3k Dec 20, 2022
This repository accompanies Test-Driven Development in Swift: Compile Better Code with XCTest and TDD

Apress Source Code This repository accompanies Test-Driven Development in Swift: Compile Better Code with XCTest and TDD by Gio Lodi (Apress, 2021). D

Apress 57 Jan 1, 2023
I built this application with unit testing and test-driven development to understand TDD theory and practice

TestDrivenDevelopment Description I built this application with unit testing and test-driven development to understand TDD theory and practice, to wri

null 1 Dec 21, 2021
Implementing and testing In-App Purchases with StoreKit2 in Xcode 13, Swift 5.5 and iOS 15.

StoreHelper Demo Implementing and testing In-App Purchases with StoreKit2 in Xcode 13, Swift 5.5, iOS 15. See also In-App Purchases with Xcode 12 and

Russell Archer 192 Dec 17, 2022
Swift Package with examples of how to tests iOS apps

GimmeTheCodeTDD A swift package with examples of unit tests in iOS development. Requirements Xcode 13 Content Dependency Injection Constructor Injecti

Dominik Hauser 8 Oct 11, 2021
A flexible mock server for automated and regression testing of iOS, Android and other apps.

Note: This document is intended as a quick introduction to Voodoo. As Voodoo has a large number of features, please refer to Voodoo's Github Wiki for

Derek Clarkson 7 Nov 23, 2022
Stub your network requests easily! Test your apps with fake network data and custom response time, response code and headers!

OHHTTPStubs OHHTTPStubs is a library designed to stub your network requests very easily. It can help you: test your apps with fake network data (stubb

Olivier Halligon 4.9k Dec 29, 2022
Swifty tool for visual testing iPhone and iPad apps. Every pixel counts.

Cribble Cribble - a tool for visual testing iPhone and iPad apps. Every pixel counts. Getting Started An example app is included demonstrating Cribble

Max Sokolov 273 Nov 4, 2022