XCFit : Full Stack BDD for iOS/macOS Apps with Swift using XCUI, Cucumberish and Fitnesse

CI Status Version License Platform

XCFit

Today, I am glad to announce “XCFit Framework” which can be used to enable BDD for iOS and macOS apps using Apple’s brand new programming language Swift and UI Testing framework. This project is inspired by Cucumberish and OCSlim project. Let’s see what are the features and usage of XCFit in this post. You can find official web page of XCFit on shashikant86.github.io

new_targets

XCFit – ‘Swift’ Your iOS/macOS apps towards BDD

XCFit a.k.a (XCUICucumberish and Fitnesse Integrations Tests) is a full stack Xcode BDD framework for Swift iOS and macOS apps. XCFit allows us to write API, UI and Acceptance Tests with Swift in human readable language using tools like Cucumber and Fitnesse in Xcode. We can still use Apple’s brand new UI Testing framework (XCUI) under the hood of Cucumberish. XCFit is fully automated solution for Cucumberish and Fitnesse. You can use 80(Fitnesse):20(Cucumberish) formula to automate Acceptance and UI Testing for better coverage and faster feedback. XCFit is available on RubyGem and CocoaPods.

XCFit Features

There are few reason you should go for XCFit


  • You can now write Given When Then and Decision Table in Xcode !! YAY.. Thanks to Cucumberish and OCSlimProject ! And XCFit automated them !!
  • You can now write/Execute all your Acdeptance Tests and UI Tests using Apple’s brand new programming language “Swift” in Xcode. Goodbye Appium, Calabash, Frank, KIF and Goodbye Ruby,Java, Python. It’s pure Swift !!
  • XCFit provides Xcode Templates to create new Cucumberish and Fitnesse targets which reduce hectic Xcode configuration steps. It’s all automated in XCFit
  • One command to set all fully automated Xcode template for Cucumberish and Fitnesse targets
  • New Template to create Gherkin Feature file in Xcode
  • Automated Cucumberish Setup in one command
  • Enhanced OCSlimProject templates and automated Fitnesse download process.
  • XCFit target templates are independent of native Xcode Test targets.
  • We can stiil use Apple’s brand new XCUI Testing framework. Its native Xcode implementation, No Appium, Calabash etc etc
  • Setup Cucumberish and Fitnesse in Xcode within few minutes !!

Quick Demo with Example App

You can clone the existing repo which has a demo app. To get started..Watch this animated GIF for the steps below

image

Run Unit, UI and Cucumberish test with Xcode. “cmd + U”. Edit Scheme if nessesary to avoid running Unit Tests for this demo. Or Run nit from command line. Update Simulator device/OS version as installed in your Xcode if nessessary

Watch out HTML and JUnit reports generated in the ‘Build/reports/’ directory with XCPretty. Install XCpretty if you havent got already.

Running Fitnesse Tests: Clean, Build and Execute

Curently XCFitDemo App is configured on TravisCI which runs both Cucumberish and Fitnesse Tests. The sample .travis.yml is in the Github repo and sample logs from TravisCI here.

Big Thanks to

  • Cucumberish : Provide native Gherkin parser for iOS Apps to enable BDD in Xcode using Given When Then. Yay!
  • OCSlimProject : Provide Xcode Templates to enable BDD with Decision Tables using Fitnesse

XCFit automated configuration of these two guys.

Version

CocoaPods — XCFit-CocoaPods : 0.1.0

RubyGems — xcfit-RubyGem : 0.8.0

Tech & Software Requirements

XCFit uses a number of open source projects to work properly. You need to have following

Hardware : You must have Mac Operating System with OSX/MacOS version > 10.9

Software:

  • Ruby – Use RVM for GEM management. Ideally Ruby > 2.X
  • Xcode – Ideally Xcode 7.X
  • RubyGems – RubyGem with Cocoapods installed
  • Curl on Mac – Might be pre-installed but worth double checking.
  • Ruby Packages : Xcpretty, Fastlane(Optional), Rake, Bundler.. Install with ‘gem install ‘

Installation

XCFit requires RubyGems to run.

You need Gulp installed globally:

Now you are good to get started with XCFit for now. Later we also need to install XCFit Pod.

Usage

Now that you can execute ‘xcfit’ command from your terminal/iTerm etc etc. Example Output Looks like this :

Setup Xcode Templates

In existing app or brand new app, we have to enable the Xcode templates for files and targets to speed up the things. Run following command from your termonal

  • This will add couple of templates to your Xcode for iOS and macOS apps. In your app if you go to ‘File—>New—>Target’

You will see new option for iOS and OSX i.e ‘XCFit’. Once Clicked on it. You will see Cucumberish API, Cucumberish UI and Fitnesse Acceptance Tests targets. As shown

image

  • Once Clicked on the target e.g ‘Cucumberish UI Test Bundle’ Xcode will create UI testing target with all the required files with Bridging hrader for Cucumberish UI Testing. Once Clicked on the target e.g ‘Fitnesse Acceptance Test Bundle’ Xcode will create UI tAcceptance test with all the required files with Bridging headers for the Fitnesse Acceptance Test. We will see that in details soon.

imageYou can watch YouTube video of XCFit setup here

  • This script also set new file type in Xcode to create new Gherking feature file

Setting up Cucumberish UI Target

3 Steps to setup Cucumberish. You don’t need to use Cocoapods to setup this target.

  • Add new target iOS/macOS project and Select “File -> New -> Target -> XCFit -> Cucumberish UI Test Bundle”. Give it a name you like e.g CucumberishUITests

Now that Xcode has created brand new target with all required Swift and Objective-C files to run Cucumberish. All Xcode setting has been done by the Xcode Template. Don’t worry about configuting anything.

  • Now that you have to CD into the Cucumberish target directory .eg CucumberishUITests

This will download Cucumberish directory from version 0.0.7 and also creats ‘Features’ directory. You can then reference those directory in Xcode. For “Cucumberish” directory select ‘Copy if needed and Create Group’ and for the “Features” directory select don’t create group or Copy if needed. Just ” create folder reference” Now Add sample Feature File to ‘Features’ directory either by File->New->Gherkin Or Just add it using your favourite text editor e.g demo.feature with content

  • Finally from the Cucumberish target ‘General’ setting select ‘Testing -> Target To Test’ and configure scheme to add Cucumeerish UI target to Test

You are done !!

You can watch YouTube video of Cucumberish Setup here

You can add more feature/Scenarios and implement steps definitions inside your Swift File. Ahmed-Ali creator of Cucumberish already implemented useful pre-defined steps have a look at this Swift file. You are free to try Page Object Pattern and all other crazy stuff to abstract and refactor your Swift code.

image

Setting up Fitnesse Acceptance Target

You can also setup Fitnesse Acceptance Tests but you need to use Cocoapod for this target.

Basically Steps are pretty much same mentioned in the OCSlimProject but most of them are automated for simplicity. Here is simple way to set Fitnesse Acceptance tests for iOS/MacOS Apps

  • Create new target, “File –> New –> Target –> XCFit –> Fitnesse Acceptance Test”. Give it a suitable name e.g ‘AcceptanceTests’
  • Create/Update Podfile with

Run ‘Pod install’ and build newly created Acceptance target from Xcode. You should see ‘LaunchFitnesse’ Script geerated and Your app should be talking to fitnesse now.

  • Now CD into FITNESSE_TARGET directory and Get Fitnesse JAR file by running

You should have fitnesse-standalone file downloaded in the target directory. You can now execute ‘LaunchFitnesse’

Now you should have Fitnesse up and runnig. Follow Paul’s article for the details on how to write acceptance tests here

You can watch YouTube video of XCFit Fitnesse here

image

Continous Integration & Test Reporting

Continous Integration is very important and you simply can’t avoid that. XCFit will be easily configured on any CI server including Apple’s own Xcode Server or very polular one Jenkins as we can generate reports in both HTML and JUnit format. As of now, you might be running tests within Xcode, but there are some awsome tools which allows us to run our tests from command line. The ‘xcodebuild’ is being used to build and test Xcode Schemes. Fastlane seems much easier option to automate everything but it’s upto you. These are long commands but you k ow better how to Script it Or Fastlane it, right ? 🙂

Curently XCFitDemo App is configured on TravisCI which runs both Cucumberish and Fitnesse Tests. The sample .travis.yml is in the Github repo and sample logs from TravisCI here. Watch out Live Execution :

image

Cucumberish

You can execute Cucumberish tests with like this for XCFitDemo App.

XCPretty can be used to generate JUnit Or HTML report as below


You can now analyse the reports generated in the ‘build/reports’ directory. You will find both HTML and JUnit reports. You can configure these reports with Xcode-Server, Jenkins, TeamCity easily in the post build setting.

image

Fitnesse

You can first build the Acceptance target

Now run the Fitnesse tests using

You can also set reports directory and generate Junit style reports inside the “XCFitDemo/FitNesseRoot/files/testResults/” directory. You can configure these reports with Xcode-Server, Jenkins, TeamCity easily in the post build setting.

What XCFit can solve in iOS/macOS team?

XCFit can bring lot of improvement the way you work. The most common problems in iOS development is explained by Paul Stringer in this article. XCFit can solve few of them

  • Better Developer/QA collabration, Early QA Involvement.

Usually,in the iOS development world, Developer build an app using Swift and Objective-C and army of manual and automated QA uses tools like Apppium, Calabash using languages like Ruby, Java, Python WTF !!. QA and Developers world is totally different. With XCFit you will be speaking same language that is “Swift”. QA can write test script as we build. No need to wait until build is handed over. Just work with developer ! Little bit of learning curve for QA but you will save developers time as they don’t to learn Java, Pyth or Ruby to help QA team

  • Quick Acceptance & UI testing setup

You will be surprised by how easily/quickly you can setup Fitnesse(Acceptance Test) and Cucumberish(UI Testing) environmant in Xcode and running your first test.

  • Getting Business people involved

As we can now able to write scenarios with Given/When/Then and decision tables. We can get business people involved to contribute to acceptance criteria. In short we got all benefits of BDD in iOS/macOS project.

  • Developers don’t need to learn Ruby or similar ! Just stick to Swift

You must be happy as you won’t need to learn other language just in case QA ask something to solve. With XCFit it’s everything Swift.

  • Fix failed attempt to automate testing by QA team

As of now your QA team might have tried all wrappers on UIAutomation e.g Appium, Calabash, Frank, KIF etc etc and keeping themselve busy writing some Ruby, Python, Java code to automate testing and all that attempts results in Slow, Brittle, Untrusted, Non-valuable UI tests causing harm to mobile development rather than good. With XCFit, everyone will be speaking same language and you can cover businness logic with lighting fast Fitnesse Tests and major journeys with UI tests. Keep it 80:20 means 80% Fitness and 20% Cucumberish or XCUI.

Source Code : GitHub

 

https://github.com/Shashikant86/XCFit

Video Demo

XCFit Xcode Setup

IMAGE ALT TEXT HERE

XCFit Cucumberish Demo

IMAGE ALT TEXT HERE

XCFit Fitnesse Demo

IMAGE ALT TEXT HERE

 

 

Let me know what you think about XCFit. Create issue on GitHub if you want more info