Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

XCFit: Full Stack BDD for Swift Using XCUI, Cucumberish, and Fitnesse

DZone's Guide to

XCFit: Full Stack BDD for Swift Using XCUI, Cucumberish, and Fitnesse

Shashikant Jagtap takes us through the features and usage of XCFit, which can be used to enable BDD for iOS and macOS using Swift.

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

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.

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 a few reasons 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

$ git clone https://github.com/Shashikant86/XCFit$ cdXCFit/XCFitDemo$ openXCFitDemo.xcworkspace

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.

$ xcodebuild -workspace XCFitDemo.xcworkspace -scheme XCFitDemo -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.3'test| xcpretty -r html -r junit --test--color

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

Running Fitnesse Tests: Clean, Build, and Execute.

$ xcodebuild -workspace XCFitDemo.xcworkspace -scheme XCFitAcceptanceTests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.3'clean build | xcpretty   $java -jar fitnesse-standalone.jar -v-o -c "FrontPage?suite&format=junit"

Currently, 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 RubyGem installed globally:

$ gem install xcfit

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 :

xcfit
Tasks:
  xcfit get_cucumberish VERSION  # Downloads Cucumberish version and Create Features directory. You must execute this from Cucumberish Xcode Target directory
  xcfit get_fitnesse             # Download Fitnesse JAR file from Internet. You must execute this from Xcode Fitnesse Acceptance Test Xcode Target directory
  xcfit help [TASK]              # Describe available tasks or one specific task
  xcfit set_xcode_templates      # Generate All Xcode Templates for the Gherkin Feature Files & targets for Cucumberish and Fitnesse
  xcfit version                  # Get the current version number

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 the following command from your terminal:

$ xcfit set_xcode_templates

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 a new option for iOS and OSX i.e ‘XCFit’. Once clicked, 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 a 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 configuring anything.

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

$ cd$CUCUMBERISH_TARGET_DIR$ xcfit get_cucumberish 0.0.7

This will download Cucumberish directory from version 0.0.7 and also creats ‘Features’ directory. You can then reference those directories in Xcode. For the “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.

Feature: Demo App Feature   Scenario: Demo Scenario   Given the app isrunning

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, the 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:

use_frameworks!   target 'AcceptanceTests'do  pod 'XCFit'end

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

  • Now CD into FITNESSE_TARGET directory and Get Fitnesse JAR file by running:
$ cd$FITNESSE_TARGET   $ xcfit get_fitnesse

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

$ ./LaunchFitnesse

Now you should have Fitnesse up and running. 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 popular 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 allow 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 up to you. These are long commands but you k ow better how to Script it Or Fastlane it, right ? 

Currently, 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

xcodebuild -workspace XCFitDemo.xcworkspace -scheme XCFitDemo -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6'test| xcpretty -r html -r junit --test--color

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.

$ open build/reports/tests.html

image

Fitnesse

You can first build the Acceptance target.

$ xcodebuild -workspace XCFitDemo.xcworkspace -scheme XCFitAcceptanceTests -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6'clean build | xcpretty

Now run the Fitnesse tests using:

$ java -jar fitnesse-standalone.jar -v-o -c "FrontPage?suite"

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 the 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 developers! Little bit of learning curve for QA but you will save developers time as they don’t to learn Java, Python, or Ruby to help the 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) environment 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 languages 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 themselves busy writing some Ruby, Python, and 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 the same language and you can cover business 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

Full Stack BDD for iOS and macOS Apps with Swift, Xcode using XCUITest, Cucumberish, FitNesse and friends.

https://github.com/Shashikant86/XCFit

0 forks.
0 open issues.
Recent commits:

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 an issue on GitHub if you want more info.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:
xcode ,bdd ,swift 2.0 ,ios ,macos ,fitnesse ,mobile ,development

Published at DZone with permission of Shashikant Jagtap, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}