BDD-style testing in Swift with Sleipnir

BDD-style testing in Swift with Sleipnir


In Objective-C people use different frameworks to write BDD-style tests for their code. Some of them are: With the inroduction of Swift we’ve decided to create BDD-style testing framework in pure Swift. After a couple weeks of implementation we are making the first public version of the framework called Sleipnir. Sleipnir is highly inspired by Cedar and allows you to write BDD-style tests in Swift:

Core principles of Sleipnir

  • 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.
We’ve found some other alternatives for Swift BDD testing, like Quick. Choosing between different solutions is a matter of preference.

Usage sample

Let’s define two classes Book and Library and write tests for them. Book contains information about an author and a title of the book.Library is a simple collection of books.First let’s test that instances of Book are initialized correctly:We’ve created a LibrarySpec class which inherits from SleipnirSpec. It has a main context and two examples which check properties of a created Book instance.An instance of Book is created in beforeAll{ } block.Sleipnir supports several blocks for specs initialization: beforeAll, afterAll, beforeEach and afterEach. All the top-level example groups in a spec should be assigned to a variable in order to evaluate:Now let’s test a behaviour of a Library class:Running those specs will produce the following command line output:In case of a failed example you will see a detailed information about the failure including file and line number:As you can see we’ve tested the behaviour of a Library class using simple expectations and matchers. Sleipnir currently supports only three matchers: equal, beTrue and beFalse, but more of them will be added soon.

What’s next

Since this is a first public release, a lot of features are not yet implemented. We have a roadmap for the nearest future which includes:
  • Distribution as a framework
  • Pending examples support
  • Focused and excluded examples/groups support implementation
  • XCode templates
  • Shared examples support
  • should syntax support
  • Specs for Sleipnir using Sleipnir
  • Wiki documentation
  • More matchers, including:
    • beNil
    • beGreaterThan, beLessThan, beInRangeOf
    • asynchronous matchers (will, willNot, after)
    • matchers on collections/strings (contains, haveCount, beginWith, endWith, etc.)
You may post your ideas or issues to the Sleipnir repo.Stay tuned for updates