Quick is a behavior-driven development framework for Swift and Objective-C. Inspired by RSpec, Specta, and Ginkgo.
// Swift
import Quick
import Nimble
class TableOfContentsSpec: QuickSpec {
override func spec() {
describe("the table of contents below") {
it("has everything you need to get started") {
let sections = TableOfContents().sections
expect(sections).to(contain("Quick: Examples and Example Groups"))
expect(sections).to(contain("Nimble: Assertions using expect(...).to"))
expect(sections).to(contain("How to Install Quick"))
}
context("if it doesn't have what you're looking for") {
it("needs to be updated") {
let you = You(awesome: true)
expect{you.submittedAnIssue}.toEventually(beTruthy())
}
}
}
}
}- Quick: Examples and Example Groups
- Using Quick in Objective-C: The Optional Shorthand Syntax
- Nimble: Assertions Using
expect(...).to - Testing UIKit with Quick
- How to Install Quick
- Including Quick in a Git Repository Using Submodules
- How to Install Quick File Templates
- Who Uses Quick
- License
Quick uses a special syntax to define examples and example groups.
Examples, defined with the it function, use assertions to demonstrate
how code should behave. These are like "tests" in XCTest.
it takes two parameters: the name of the example, and a closure.
The examples below specify how the Dolphin class should behave.
A new dolphin should be smart and friendly:
// Swift
import Quick
import Nimble
class DolphinSpec: QuickSpec {
override func spec() {
it("is friendly") {
expect(Dolphin().isFriendly).to(beTruthy())
}
it("is smart") {
expect(Dolphin().isSmart).to(beTruthy())
}
}
}// Objective-C
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
QuickSpecBegin(DolphinSpec)
it(@"is friendly", ^{
expect(@([[Dolphin new] isFriendly])).to(beTruthy());
});
it(@"is smart", ^{
expect(@([[Dolphin new] isSmart])).to(beTruthy());
});
QuickSpecEndDescriptions can use any character, including characters from languages besides English, or even emoji! ✌️ 😎
Example groups are logical groupings of examples. Example groups can share setup and teardown code.
To specify the behavior of the Dolphin class's click method--in
other words, to test the method works--several it examples can be
grouped together using the describe function. Grouping similar
examples together makes the spec easier to read:
// Swift
import Quick
import Nimble
class DolphinSpec: QuickSpec {
override func spec() {
describe("a dolphin") {
describe("its click") {
it("is loud") {
let click = Dolphin().click()
expect(click.isLoud).to(beTruthy())
}
it("has a high frequency") {
let click = Dolphin().click()
expect(click.hasHighFrequency).to(beTruthy())
}
}
}
}
}// Objective-C
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
QuickSpecBegin(DolphinSpec)
describe(@"a dolphin", ^{
describe(@"its click", ^{
it(@"is loud", ^{
Click *click = [[Dolphin new] click];
expect(@(click.isLoud)).to(beTruthy());
});
it(@"has a high frequency", ^{
Click *click = [[Dolphin new] click];
expect(@(click.hasHighFrequency)).to(beTruthy());
});
});
});
QuickSpecEndExample groups don't just make the examples clearer, they're also useful for sharing setup and teardown code among examples in a group.
In the example below, the beforeEach function is used to create a brand
new instance of a dolphin and its click before each example in the group.
This ensures that both are in a "fresh" state for every example:
// Swift
import Quick
import Nimble
class DolphinSpec: QuickSpec {
override func spec() {
describe("a dolphin") {
var dolphin: Dolphin?
beforeEach {
dolphin = Dolphin()
}
describe("its click") {
var click: Click?
beforeEach {
click = dolphin!.click()
}
it("is loud") {
expect(click!.isLoud).to(beTruthy())
}
it("has a high frequency") {
expect(click!.hasHighFrequency).to(beTruthy())
}
}
}
}
}// Objective-C
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
QuickSpecBegin(DolphinSpec)
describe(@"a dolphin", ^{
__block Dolphin *dolphin = nil;
beforeEach(^{
dolphin = [Dolphin new];
});
describe(@"its click", ^{
__block Click *click = nil;
beforeEach(^{
click = [dolphin click];
});
it(@"is loud", ^{
expect(@(click.isLoud)).to(beTruthy());
});
it(@"has a high frequency", ^{
expect(@(click.hasHighFrequency)).to(beTruthy());
});
});
});
QuickSpecEndSharing setup like this might not seem like a big deal with the dolphin example, but for more complicated objects, it saves a lot of typing!
To execute code after each example, use afterEach.
Dolphins use clicks for echolocation. When they approach something particularly interesting to them, they release a series of clicks in order to get a better idea of what it is.
The tests need to show that the click method behaves differently in
different circumstances. Normally, the dolphin just clicks once. But when
the dolphin is close to something interesting, it clicks several times.
This can be expressed using context functions: one context for the
normal case, and one context for when the dolphin is close to
something interesting:
// Swift
import Quick
import Nimble
class DolphinSpec: QuickSpec {
override func spec() {
describe("a dolphin") {
var dolphin: Dolphin?
beforeEach { dolphin = Dolphin() }
describe("its click") {
context("when the dolphin is not near anything interesting") {
it("is only emitted once") {
expect(dolphin!.click().count).to(equal(1))
}
}
context("when the dolphin is near something interesting") {
beforeEach {
let ship = SunkenShip()
Jamaica.dolphinCove.add(ship)
Jamaica.dolphinCove.add(dolphin)
}
it("is emitted three times") {
expect(dolphin!.click().count).to(equal(3))
}
}
}
}
}
}// Objective-C
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
QuickSpecBegin(DolphinSpec)
describe(@"a dolphin", ^{
__block Dolphin *dolphin = nil;
beforeEach(^{ dolphin = [Dolphin new]; });
describe(@"its click", ^{
context(@"when the dolphin is not near anything interesting", ^{
it(@"is only emitted once", ^{
expect(@([[dolphin click] count])).to(equal(@1));
});
});
context(@"when the dolphin is near something interesting", ^{
beforeEach(^{
[[Jamaica dolphinCove] add:[SunkenShip new]];
[[Jamaica dolphinCove] add:dolphin];
});
it(@"is emitted three times", ^{
expect(@([[dolphin click] count])).to(equal(@3));
});
});
});
});
QuickSpecEndFor examples that don't pass yet, use pending. Pending examples
are not run, but are printed out along with the test results.
The example below marks the cases in which the dolphin is close to something interesting as "pending"--perhaps that functionality hasn't been implemented yet, but these tests have been written as reminders that it should be soon:
// Swift
pending("when the dolphin is near something interesting") {
// ...none of the code in this closure will be run.
}// Objective-C
pending(@"when the dolphin is near something interesting", ^{
// ...none of the code in this closure will be run.
});Examples and groups can also be marked as pending by using
xdescribe, xcontext, and xit:
// Swift
xdescribe("its click") {
// ...none of the code in this closure will be run.
}
xcontext("when the dolphin is not near anything interesting") {
// ...none of the code in this closure will be run.
}
xit("is only emitted once") {
// ...none of the code in this closure will be run.
}// Objective-C
xdescribe(@"its click", ^{
// ...none of the code in this closure will be run.
});
xcontext(@"when the dolphin is not near anything interesting", ^{
// ...none of the code in this closure will be run.
});
xit(@"is only emitted once", ^{
// ...none of the code in this closure will be run.
});Some test setup needs to be performed before any examples are
run. For these cases, use beforeSuite and afterSuite.
In the example below, a database of all the creatures in the ocean is created before any examples are run. That database is torn down once all the examples have finished:
// Swift
import Quick
class DolphinSpec: QuickSpec {
override func spec() {
beforeSuite {
OceanDatabase.createDatabase(name: "test.db")
OceanDatabase.connectToDatabase(name: "test.db")
}
afterSuite {
OceanDatabase.teardownDatabase(name: "test.db")
}
describe("a dolphin") {
// ...
}
}
}// Objective-C
#import <Quick/Quick.h>
QuickSpecBegin(DolphinSpec)
beforeSuite(^{
[OceanDatabase createDatabase:@"test.db"];
[OceanDatabase connectToDatabase:@"test.db"];
});
afterSuite(^{
[OceanDatabase teardownDatabase:@"test.db"];
});
describe(@"a dolphin", ^{
// ...
});
QuickSpecEndYou can specify as many
beforeSuiteandafterSuiteas you like. AllbeforeSuiteclosures will be executed before any tests run, and allafterSuiteclosures will be executed after all the tests are finished. There is no guarantee as to what order these closures will be executed in.
In some cases, the same set of specifications apply to multiple objects.
For example, consider a protocol called Edible. When a dolphin
eats something Edible, the dolphin becomes happy. Mackerel and
Cod are both edible. Quick allows you to easily test that a dolphin is
happy to eat either one.
The example below defines a set of "shared examples" for "something edible", and specifies that both mackerel and cod behave like "something edible":
// Swift
import Quick
import Nimble
class EdibleSharedExamples: QuickSharedExampleGroups {
override class func sharedExampleGroups() {
sharedExamples("something edible") { (sharedExampleContext: SharedExampleContext) in
it("makes dolphins happy") {
let dolphin = Dolphin(happy: false)
let edible = sharedExampleContext()["edible"]
dolphin.eat(edible)
expect(dolphin.isHappy).to(beTruthy())
}
}
}
}
class MackerelSpec: QuickSpec {
override func spec() {
var mackerel: Mackerel! = nil
beforeEach {
mackerel = Mackerel()
}
itBehavesLike("something edible") { ["edible": mackerel] }
}
}
class CodSpec: QuickSpec {
override func spec() {
var cod: Cod! = nil
beforeEach {
cod = Cod()
}
itBehavesLike("something edible") { ["edible": cod] }
}
}// Objective-C
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
QuickSharedExampleGroupsBegin(EdibleSharedExamples)
qck_sharedExamples(@"something edible", ^(QCKDSLSharedExampleContext exampleContext) {
it(@"makes dolphins happy") {
Dolphin *dolphin = [[Dolphin alloc] init];
dolphin.happy = NO;
id<Edible> edible = exampleContext()[@"edible"];
[dolphin eat:edible];
expect(dolphin.isHappy).to(beTruthy())
}
});
QuickSharedExampleGroupsEnd
QuickSpecBegin(MackerelSpec)
__block Mackerel *mackerel = nil;
qck_beforeEach(^{
mackerel = [[Mackerel alloc] init];
});
qck_itBehavesLike(@"someting edible", ^{ return @{ @"edible": mackerel }; });
QuickSpecEnd
QuickSpecBegin(CodSpec)
__block Mackerel *cod = nil;
qck_beforeEach(^{
cod = [[Cod alloc] init];
});
qck_itBehavesLike(@"someting edible", ^{ return @{ @"edible": cod }; });
QuickSpecEndShared examples can include any number of it, context, and
describe blocks. They save a lot of typing when running
the same tests against several different kinds of objects.
In some cases, you won't need any additional context. In Swift, you can
simply use sharedExampleFor closures that take no parameters. This
might be useful when testing some sort of global state:
// Swift
import Quick
sharedExamplesFor("everything under the sea") {
// ...
}
itBehavesLike("everything under the sea")In Objective-C, you'll have to pass a block that takes a
QCKDSLSharedExampleContext, even if you don't plan on using that argument. Sorry, but that's the way the cookie crumbles! :cookie: :bomb:
Quick works equally well in both Swift and Objective-C.
Importing Quick in an Objective-C file defines macros such as it,
context, and describe. It's possible that the project you are
testing also defines symbols with these same names. In that case, you
can avoid namespace collision by turning off Quick's optional "shorthand" syntax:
#define QUICK_DISABLE_SHORT_SYNTAX 1
#import <Quick/Quick.h>
QuickSpecBegin(DolphinSpec)
// ...
QuickSpecEndYou must define the QUICK_DISABLE_SHORT_SYNTAX macro before
importing the Quick header.
Quick provides an easy language to define examples and example groups. Within those examples, Nimble provides a simple language to define expectations--that is, to assert that code behaves a certain way, and to display a test failure if it doesn't.
Nimble expectations use the expect(...).to syntax:
// Swift
import Nimble
expect(person.greeting).to(equal("Oh, hi."))
expect(person.greeting).notTo(equal("Hello!"))
expect(person.isHappy).toEventually(beTruthy())// Objective-C
#import <Nimble/Nimble.h>
expect(person.greeting).to(equal(@"Oh, hi."));
expect(person.greeting).notTo(equal(@"Hello!"));
expect(@(person.isHappy)).toEventually(beTruthy());You can find much more detailed documentation on Nimble, including a full set of available matchers and details on how to perform asynchronous tests, in the project's README.
Quick can be used for testing UIKit interaction as well. Say, for example, we have a DolphinTableViewController that displays one cell with label Bottlenose. We want to test that the cell gets displayed when the view is loaded. Additionally, we would like to delete the row upon selecting it. An approach might be:
// Swift
import UIKit
import Quick
import Nimble
class DolphinTableViewControllerSpecs: QuickSpec {
override func spec() {
var viewController: DolphinTableViewController!
beforeEach {
viewController = DolphinTableViewController()
}
describe("viewDidLoad") {
beforeEach {
viewController.view
// Accessing the view property causes the UIKit framework to trigger the necessary methods to render the view.
}
it("loads the table view with one cell") {
let tableView = viewController.tableView
var indexPath = NSIndexPath(forRow: 0, inSection: 0)
var cell = viewController.tableView(tableView, cellForRowAtIndexPath: indexPath)
expect(cell.textLabel?.text).to(equal("Bottlenose"))
}
}
describe("didSelectRowAtIndexPath") {
beforeEach {
viewController.view
}
it("deletes the selected row and reloads the tableView's data") {
let tableView = viewController.tableView
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
viewController.tableView(tableView, didSelectRowAtIndexPath: indexPath)
var cell = viewController.tableView(tableView, cellForRowAtIndexPath: indexPath)
expect(cell.textLabel?.text).to(beNil())
}
}
}
}// Objective-C
#import <UIKit/UIKit.h>
#import <Quick/Quick.h>
#import <Nimble/Nimble.h>
QuickSpecBegin(DolphinTableViewControllerSpec)
qck_describe(@"viewDidLoad") {
__block DolphinTableViewController *viewController = nil;
qck_beforeEach(^{
viewController = [[DolphinTableViewController alloc] init];
});
qck_it(@"loads the table view with three types of dolphin", ^{
qck_beforeEach(^{
[viewController view];
// Accessing the view property causes the UIKit framework to trigger the necessary methods to render the view.
});
UITableView *tableView = [viewController tableView];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell = [viewController tableView:tableView cellForRowAtIndexPath:indexPath];
expect(@([[cell textLabel] text])).to(equal(@"Bottlenose"));
});
}
qck_describe(@"didSelectRowAtIndexPath") {
__block DolphinTableViewController *viewController = nil;
qck_beforeEach(^{
viewController = [[DolphinTableViewController alloc] init];
[viewController view];
});
qck_it(@"deletes the selected row and reloads the tableView's data", ^{
UITableView *tableView = [viewController tableView];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[viewController tableView:tableView didSelectRowAtIndexPath:indexPath];
UITableViewCell *cell = [viewController tableView:tableView cellForRowAtIndexPath:indexPath];
expect(@([[cell textLabel] text])).to(beNil());
});
}
QuickSpecEndThis module is beta software, it currently supports Xcode 6 Beta 4.
Quick provides the syntax to define examples and example groups. Nimble
provides the expect(...).to assertion syntax. You may use either one,
or both, in your tests.
To use Quick and Nimble to test your iOS or OS X applications, follow these 4 easy steps:
- Clone the Quick and Nimble repositories
- Add
Quick.xcodeprojandNimble.xcodeprojto your test target - Link
Quick.frameworkandNimble.framework - Start writing specs!
Example projects with this complete setup is available in the
Examples directory.
git clone [email protected]:Quick/Quick.git Vendor/Quick
git clone [email protected]:Quick/Nimble.git Vendor/NimbleRight-click on the group containing your application's tests and
select Add Files To YourApp....
Next, select Quick.xcodeproj, which you downloaded in step 1.
Once you've added the Quick project, you should see it in Xcode's project navigator, grouped with your tests.
Follow the same steps for Nimble.xcodeproj.
Link the Quick.framework during your test target's
Link Binary with Libraries build phase. You should see two
Quick.frameworks; one is for OS X, and the other is for iOS.
Do the same for the Nimble.framework.
If you run into any problems, please file an issue.
The best way to include Quick in a Git repository is by using Git submodules. Git submodules are great because:
- They track exactly which version of Quick is being used
- It's easy to update Quick to the latest--or any other--version
To use Git submodules, follow the same steps as above, except instead of cloning the Quick and Nimble repositories, add them to your project as submodules:
mkdir Vendor # you can keep your submodules in their own directory
git submodule add [email protected]:Quick/Quick.git Vendor/Quick
git submodule add [email protected]:Quick/Nimble.git Vendor/Nimble
git submodule update --init --recursiveIf you ever want to update the Quick submodule to latest version, enter the Quick directory and pull from the master repository:
cd Vendor/Quick
git pull --rebase origin masterYour Git repository will track changes to submodules. You'll want to commit the fact that you've updated the Quick submodule:
git commit -m "Updated Quick submodule"After other people clone your repository, they'll have to pull down the
submodules as well. They can do so by running the git submodule update
command:
git submodule update --init --recursiveYou can read more about Git submodules here. To see examples of Git submodules in action, check out any of the repositories linked to in the "Who Uses Quick" section of this guide.
The Quick repository includes file templates for both Swift and Objective-C specs.
Quick templates can be installed via Alcatraz, a package manager for Xcode. Just search for the templates from the Package Manager window.
To manually install the templates, just clone the repository and
run the templates:install rake task:
$ git clone [email protected]:Quick/Quick.git
$ rake templates:installUninstalling is easy, too:
$ rake templates:uninstall- https://github.com/jspahrsummers/RXSwift
- https://github.com/artsy/eidolon
- https://github.com/AshFurrow/Moya
- https://github.com/nerdyc/Squeal
- https://github.com/pepibumur/SugarRecord
Add an issue or tweet if you'd like to be added to this list.
MIT license. See the LICENSE file for details.