Behaviour Driven Development Frameworks for Swift

Recently, there are various companies looking BDD supporting tools for iOS apps written in Swift. As there is no official support from Cucumber for Swift so most of them are struggling to find right BDD tool for Swift project. This post will highlight some of the tools that support BDD in the Swift applications. BDD a.k.a Behaviour Driven Development is a process of developing an application by using executable specifications in Gherkin or similar so that programmers can use those specifications for development and business people can use them requirement specification or living documentation. BDD is an outside-in development methodology. You can read more about BDD here.

Behaviour Driven Development in the iOS world was always challenging because of lacking proper BDD tools like Cucumber in Swift. In the website development, there is language specific option for BDD options. There are some tools which parse Gherkin language syntax (Given/When/Then) and we can execute BDD scenarios e.g Cucumber for Ruby, CucumberJS for JavaScript, Cucumber-JVM for Java, SpecFlow for C#, Behat for PHP etc. There are also some of the tools for Spec level BDD like RSpec for Ruby, PHPSpec for PHP, Jasmine for JavaScript. However, in the iOS world, there are very limited options to perform BDD, especially for the Swift. In this post, we will explore what are the current BDD option for Swift iOS apps.

Gherkin Style BDD in Swift

Gherkin Style BDD might be easily achieved with tools like Cucumber with Appium or Calabash but to support BDD inside Xcode isn’t straightforward. BDD in the iOS world was always challenging because of lacking proper BDD tools like Cucumber in Swift. There isn’t any Gherkin parser available in Swift so Cucumber community suggested to use Objective-C Gherkin parser. There are multiple BDD tools to write lower level tests mentioned here, but in order to write UI level tests, there are very few options which may not have all the core features of Cucumber in Ruby.

• XCTest-Gherkin

XCTest-Gherkin is another tool that can be used to achieve BDD inside Xcode projects. There is good information about its features and limitations on the README file. Its pure swift representation and we can still have GWT(Given When Then) Written in the String format but the major drawback is we cannot write or execute .feature  files and lacks many core cucumber features like tags, filtering features. You need to get XCTest-Gherkin as a dependency for your UI Testing target using CocoaPods or Carthage.

• XCFit

XCFit is another pure Swift implementation to support BDD inside Xcode projects. It also gives Xcode templates to get started with BDD, Page Object Pattern and Protocol Oriented pattern. XCFit has some pre-defined steps inside Swift frameworks but the quick benefit comes from Xcode templates. More information on Protocol Oriented BDD here. Again major drawback is we cannot write or execute .feature files and lacks many core cucumber features like tags, filtering features.

• Cucumberish

Cucumberish is a test framework that can be used to write features in Gherkin format and can be executed from Xcode. Cucumberish is written Objective-C and uses Objective-C Gherkin parser from Cucumber repository. Its great to be able to execute Gherkin feature files from Xcode but Cucumberish  has some bugs and needs workarounds for most cases. Also its bit tricky to introduce Objective-C dependency in the fully Swift Project. Cucumberish can be imported to UI Test target using CocoaPods or Carthage.

• Ploughman

Ploughman is another test runner for the Gherkin language where we can write and execute feature file from command line. There is good information on the README file of the Ploughman. This lacks the lot of features of the core Cucumber like examples, tags, and filters. This package can be imported using Swift Package Manager.

• SwiftGherkin

SwiftGherkin is another Swift package for working with gherkin based .feature  files. This is newly created package having support for Features, Scenarios, Scenario Outlines and Steps, however, there will be support for all other core cucumber features in future. You can stay tuned with this repository for more news on this package. This package can be used with Swift Package Manager only.

Spec Style BDD in Swift

Similar to RSpec for Ruby, PHPSpec for PHP, Jasmine for JavaScript, there are few tools where we can write the specification for the lower level implementation. We will see those spec level BDD options in Swift.

• Sleipnir

Sleipnir is a spec level BDD-style framework for Swift which is highly inspired by Cedar. from Objective-C. This is probably the first ever BDD style framework announced for the Swift. You can read more about the inspiration and introduction of BDD to Swift in this blog. However, Sleipnir is not using XCTest framework and its not supported for iOS at the moment. It isn’t also properly supported by CocoaPods.

• Quick

Quick  is very mature RSpec style framework for Swift at the moment. Quick comes with Nimble matcher which make the DSL much readable. Quick is compatible with CocoaPods, Carthage, and Swift Package Manager. It’s been sometimes used for as a replacement for the Apple own XCTest framework as specs become much more readable using the combination of Quick and Nimble. There are many popular projects which use Quick and some of them listed on this page.

Conclusion

Behaviour Driven Development is the collaborative process which isn’t dependent on tools but having those BDD supporting tools make collaboration much easier. The goal of this post was to list major tools available to support BDD for Swift projects. Please let me know if I missed any tools.