• Stars
    star
    847
  • Rank 53,812 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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 child of Loki and SvaΓ°ilfari, is described as the best of all horses, and is sometimes ridden to the location of Hel.

class SampleSpec : SleipnirSpec {
    var spec : () = describe("Horse") {
        context("usual") {
            it("is not awesome") {
                let usualHorse = UsualHorse()
                usualHorse.legsCount.should.equal(4)
                expect(usualHorse.isAwesome()).to(beFalse())
            }
        }
        
        context("Sleipnir") {
            it("is awesome") {
                let sleipnirHorse = Sleipnir()
                sleipnirHorse.legsCount.should.equal(8)
                expect(sleipnirHorse.isAwesome()).to(beTrue())
            }
        }
    }
}

Core principles

  • Sleipnir is not dependent of NSObject, it is pure Swift BDD testing framework
  • Sleipnir is not using XCTest
  • Sleipnir has nice command line output and support for custom test reporters
  • Other features, like seeded random tests invocation, focused and excluded examples/groups, etc.

Installation

Manually (preferred way)

  1. Add Sleipnir as a submodule: git submodule add https://github.com/railsware/sleipnir ThirdParty/Sleipnir
  2. Drag'n'drop Sleipnir.xcodeproj to your test target
  3. Link Sleipnir.framework
  4. Start writing specs!

Manually (from XCode Project template)

  1. Clone Sleipnir repo git clone https://github.com/railsware/sleipnir /tmp/Sleipnir
  2. Execute the following command cd /tmp/Sleipnir && make install_templates

The command will install templates for OSX and iOS projects and Spec file template as well.

Note: this way you should manage framework updates on your own. Try to reinstall templates before creating new project/target from "old" ones

Via CocoaPods

You can install statically built Sleipnir.framework into you project simply by adding it to the Podfile

pod 'Sleipnir'

Note: it is experimental way
Current build does not work on iPhone Simulator, but works for OSX and iOS Devices

Usage sample

See LibrarySpec file in Sample project.

Writing specs

All spec classes should inherit from SleipnirSpec.

Root ExampleGroups in a spec should be assigned to some void variable. This allows specs to initialize correctly:

import Sleipnir

class SomeSpec : SleipnirSpec {
 
    let someSpec : () = describe("Some spec") {
        it("should pass") {
            expect(1).to(equal(1))
        }
    }
    
}

Running specs

In order to run your specs you should invoke Runner.run() from the main.swift of your test target.

The default test runner will produce the following command line output, indicating all the failures and some meta information about each failure:

Running With Random Seed: 1234

.....F..


FAILURE Some spec should pass:
/Path/To/Your/Specs/TestSpec.swift:16 Expected <1> to equal <2>


Finished in 0.0075 seconds

8 examples, 1 failures

Seeded random specs run

All examples would run in random order with the random seed specified in output. If you would like to re-run tests in the same order you should pass a seed to run() method:

Runner.run(seed: 1234)

Examples and ExampleGroups

ExampleGroups are created with describe or context keywords. Within the block passed to ExampleGroup you can declare examples using the it keyword.

ExampleGroups can be nested in order to create clean structure of your tests.

Under the hood describe method creates an instance of ExampleGroup and evaluates the block passed to it. Blocks passed to examples are evaluated dynamically during the test execution.

Setup/Teardown code

Sleipnir supports some hooks to execute before or after examples. This allows to share some setup/teardown code between examples.

beforeEach block will be executed before each example in the current group and all nested groups.

afterEach block will be executed after each example.

class SomeSpec : SleipnirSpec {
 
    let someSpec : () = describe("Some spec") {
        var someArray: [Int]?
        beforeEach {
            someArray = [1, 2, 3]
        }
        
        afterEach {
            someArray = nil
        }
        
        it("should pass") {
            expect(someArray).toNot(beNil())
            expect(someArray).to(contain(3))
        }
    }
    
}

You can also specify global setup/teardown blocks using beforeAll and afterAll keywords. They will run once before or after all examples in the current group and all the nested groups.

Focused and excluded specs

You can specify examples and example groups to be focused by placing f letter before declaration: fdescribe, fcontext, fit. In this case the spec runner will only run focused examples/example groups and ignore all the others.

You can also mark an example or example group as pending. It won't run but will be printed along with the test results.
To mark something as pending add an x letter before declaration: xdescribe, xcontext, xit.
Example can also be marked as pending by passing PENDING instead of spec block:

it("is pending", PENDING)

Shared example groups

Sleipnir supports extracting common specs through shared example groups.
They can include any number of it, context, describe, before and after blocks.
You can pass example-specific state into the shared example group through sharedContext dictionary.

var nonNilObject : () =
    sharedExamplesFor("a non-nil object") { (sharedContext : SharedContext) in
        var object: AnyObject?
        beforeEach {
            object = sharedContext()["object"]
        }
        
        it("should not be nil") {
            expect(object).toNot(beNil())
        }
    }

var spec : () = context("A non-nil object") {
    let someObject: String = "Some Object"
    itShouldBehaveLike("a non-nil object", { ["object" : someObject] })
}

If you don't need any context, you can use closures without parameters:

sharedExamplesFor("some awesome stuff") {
    it("should be awesome") {
        //...
    }
}

describe("Some stuff") {
    itShouldBehaveLike("some awesome stuff")
}

Expectations

Sleipnir supports defining expectations using expect(someValue/expession).to(SomeMatcher) syntax.

expect(true).to(beTrue())

expect method supports passing values or a block of code in a closure:

expect {
    var x = 1
    x++
    return x
}.to(equal(2))

Should syntax

In addition to the expect syntax, Sleipnir supports the should syntax:

actual.should.equal(expected)
[1, 2, 3].shouldNot.contain(4)

See detailed information on the should syntax and its usage

Available matchers

Equal

Values must be Equatable, Comparable or derive from NSObject.

expect([1,2,3]).to(equal([1,2,3]))
expect("some string").toNot(equal("another string"))
expect(1) == 1
BeNil
expect(nil).to(beNil())
expect("some string").toNot(beNil())
BeTrue/BeFalse
expect(true).to(beTrue())
expect(false).to(beFalse())
BeGreaterThan/BeLessThan

Values must be Comparable.

expect(3).to(beGreaterThan(1))
expect(3) > 1

expect(1).to(beLessThan(3))
expect(1) < 3
BeGreaterThanOrEqualTo/BeLessThanOrEqualTo

Values must be Comparable.

expect(3).to(beGreaterThanOrEqualTo(1))
expect(3) >= 1
expect(1) >= 1

expect(1).to(beLessThanOrEqualTo(3))
expect(1) <= 3
expect(1) <= 1
Collections/String matchers

Sleipnir supports some matchers on collections and strings:

Contain

Matches if an item is in the container. Supports Swift collections with Equatable elements, NSArrays, NSSets and Strings.

expect([1,2,3]).to(contain(1))
expect("some string").toNot(contain("another"))
BeginWith/EndWith

Matches if the container begins/ends with the specified element. Supports Swift collections with Equatable elements, NSArrays and Strings.

expect([1,2,3]).to(beginWith(1))
expect("some string").to(endWith("string"))
BeEmpty

Matches if a container is empty.

expect("").to(beEmpty())
expect([1,2,3]).toNot(beEmpty())

TODO

  • Ease of distribution (CocoaPods probably)
  • XCode templates
  • Shared examples support
  • should syntax support
  • asynchronous matchers (will, willNot, after)

Who uses Sleipnir

  • SGL - Swift Generic Library

Contributing

Please read the Contributor Guide.

License

Licensed under MIT license. See the LICENSE file for details.

More Repositories

1

upterm

A terminal emulator for the 21st century.
TypeScript
19,318
star
2

js-routes

Brings Rails named routes to javascript
Ruby
1,606
star
3

bozon

πŸ›  Command line tool for building, testing and publishing modern Electron applications
JavaScript
758
star
4

BloodMagic

BloodMagic is a framework, which gives you a way to create custom property attributes.
Objective-C
317
star
5

global

"Global" provides accessor methods for your configuration data
Ruby
282
star
6

applepie

Semantic and Modular CSS Toolkit
CSS
280
star
7

rack_session_access

Rack middleware that provides access to rack.session environment
Ruby
259
star
8

caphub

Generate centralized capistrano skeleton for multiple deployment
Ruby
230
star
9

smt_rails

Shared mustache templates for rails 3.
Ruby
110
star
10

http_logger

Log your http api calls just like SQL queries
Ruby
106
star
11

rspec-example_steps

Given/When/Then steps for RSpec examples
Ruby
85
star
12

sht_rails

Shared handlebars templates for Rails 3
Ruby
76
star
13

actionmailer-balancer

A Ruby gem to send your ActionMailer mail through one of several delivery methods, selected by weight.
Ruby
76
star
14

capistrano-multiconfig

Capistrano extension that allows to use multiple configurations
Ruby
66
star
15

mailtrap-nodejs

Official mailtrap.io Node.js client
TypeScript
58
star
16

passenger-initscript

Manage multiple passenger instances
Shell
54
star
17

skypekit

Ruby FFI interface to libskypekit C library
Ruby
47
star
18

newrelic_platform_plugins

Ruby
41
star
19

piro

PiRo - it's Rocket for you Pivotal Tracker account
CoffeeScript
40
star
20

zero_deploy

Significantly improves typical deployment speed
Ruby
40
star
21

mailtrap-php

The official mailtrap.io PHP client
PHP
35
star
22

capistrano-calendar

Deployment event creation on (google) calendar service
Ruby
29
star
23

capistrano-patch

Capistrano patch recipes
Ruby
29
star
24

scaffy.railsware.com

Approach for writing and organizing your CSS for large-scale projects
JavaScript
29
star
25

libskypekit

Thread-safe C library with synchronous API using asynchronous C++ SkypeKit SDK
C
19
star
26

backfiller

The backfill machine for database records with null columns
Ruby
16
star
27

capistrano-ci

Ruby
16
star
28

indeed

Indeed.com integration plugin
Ruby
16
star
29

mailtrap-ruby

The official mailtrap.io Ruby client
Ruby
15
star
30

RBRouteBuilder

Build routes without strings and headache
C++
15
star
31

sprinkle_recipes

Railsware sprinkle recipes
Ruby
14
star
32

capistrano-uptodate

Capistrano extension that automatically check local repository with remote repository
Ruby
14
star
33

activeresource-persistent

HTTP persistent connection support for ActiveResource
Ruby
11
star
34

haproxy-slow-fast-request-balancer

HAProxy as "Slow-Fast" Request Balancer
10
star
35

generator-electron-app

Yeoman generator to scaffold Electron app
JavaScript
9
star
36

scout-app-plugins

Useful plugins for the Scout Server Monitoring and Reporting Tool
Ruby
9
star
37

mailtrap-python

Official mailtrap.io Python client
Python
8
star
38

web-bundler

WebResourceBundler bundling particular resource (css or js) in one file. Encoding images in base64 and putting then in css directly.
Ruby
8
star
39

gcal4ruby

Author: Mike Reich. GCal4Ruby is a Ruby Gem that can be used to interact with the current version of the Google Calendar API. GCal4Ruby provides the following features: Create and edit calendar events, Add and invite users to events, Set reminders, Make recurring events.
Ruby
8
star
40

db_structure_ext

Extended rails tasks db:structure:dump/load that supports mysql views/triggers/routines
Ruby
7
star
41

github-actions

A collection of GitHub actions used at Railsware
JavaScript
7
star
42

jdt

JSON Driven Templates
JavaScript
7
star
43

shelltoad

Command line interface for airbrake (http://airbrake.io/)
Ruby
7
star
44

i18n_template

18nTemplate is made to extract phrases and translate html/xhtml/xml document or erb templates
Ruby
6
star
45

capistrano-changelog

Chagelog based on Git commits with a Pivotal story tags
Ruby
5
star
46

go-global

Golang configuration reader for AWS Parameter Store and more
Go
4
star
47

chain_flow

Helps to refactor complex data processing
Ruby
4
star
48

fakes3server

Fake AWS S3 server for local development
Go
4
star
49

jOverlay

Overlay jQuery plugin
JavaScript
4
star
50

dev_vagrant_box

Vagrant box for development (ruby, rails)
4
star
51

yaml_settings

YamlSettings is a simple configuration / settings solution that uses an ERB enabled YAML file.
Ruby
3
star
52

multiversion

Use Bundler and RVM to test your library against different gem versions and/or ruby versions.
Ruby
3
star
53

clicktale

Clicktale rails plugin by Railsware
Ruby
2
star
54

ui-library

UI Library Template
CSS
2
star
55

railsware.github.com

CSS
2
star
56

capybara-feature_helpers

Ruby
2
star
57

backbone_showcase

Ruby
2
star
58

em-rest-client

EventMachine::HttpRequest adapter for HTTP REST client
Ruby
2
star
59

newrelic_em_http

New Relic EM::HTTP instrumentation
Ruby
2
star
60

capistrano-strategy-copy-partial

Capistrano deploy strategy to transfer subdirectory of repository
Ruby
2
star
61

hbase-driver

Small and handy Ruby driver for HBase
Ruby
2
star
62

acme-aws-lambda

AWS Lambda function to generate Letsencrypt certificates (need AWS S3 and Route53)
Ruby
2
star
63

showroom

Ruby
2
star
64

email_templates

HTML
2
star
65

rest_facebook

Lightweight ruby facebook client
Ruby
1
star
66

plunger

Code review tool
Python
1
star
67

cat-aws-ssm-param

Go
1
star
68

rw-study-wishlist

Ruby
1
star
69

template

Rails project template
JavaScript
1
star
70

chef-rwci

Ruby
1
star
71

office-iot

Objective-C
1
star
72

simple_on_couch

Example of CouchApp application for RW articles
JavaScript
1
star
73

scaffy

Repo moved to scaffy.railsware.com
1
star
74

highrise_assist

Assist for 37signals' highrise
Ruby
1
star
75

sidekiq_unique_retries

Unique Retries for Sidekiq
Ruby
1
star
76

skypekit_pure

Skypekit on pure ruby
Ruby
1
star
77

aws-ecs-tools

Ruby
1
star
78

capybara_mock

CapybaraMock
Ruby
1
star
79

handlebars_assets_i18n

Very simple handlebars_assets internationalization for .hamlbars and .slimbars templates
Ruby
1
star
80

paypal-sdk-http-adapters

HTTP Adapters for PayPal SDK
Ruby
1
star
81

gdoc_mapreduce

JavaScript
1
star
82

harvest_report

Harvert reports for non-admin harvest users
Ruby
1
star
83

rest-client-adapters

RestClient Adapters
Ruby
1
star
84

versioned_item

Ruby
1
star
85

cordova-baxi-plugin

Java
1
star
86

column_info_reset

Reset ActiveRecord column info when unknown column exception occurs
Ruby
1
star
87

gdoc_pivotal

Gdoc updater for pivotal
JavaScript
1
star
88

mailtrap-examples

Mailtrap repo examples
HTML
1
star
89

mailtrap-elixir

The official mailtrap.io Elixir client
Elixir
1
star