Quick and Nimble: Behavior-driven development in Swift
In modern software development, we are having faster iterations, continuous integration, continuous deployment and even daily builds being deployed into production, automated testing has become essential to our project’s workflow in the sense that it makes all this possible by being a validation step which can give us a certain guarantee that we didn’t break some already existing-working flow of the production application with a new patch. Of course, automated tests don’t mean no bugs at all, but in some cases, it can prevent us from deploying a bug into production that is going to impact our clients.
In today’s post, we are going to make a high-level overview of testing and behavior-driven development in Swift with Quick and Nimble trying to understand what are all this and why it may be a good thing to use in some scenarios.
So, let’s get it started…
Behavior-driven Development
Behavior-driven development (BDD) is a set of practices in the software development that focus on feature specification and application behavior where the goal is to have a notation (given, when, then) that is understandable to the developers, testers and business people.
One of the major advantages of BDD is to have a specification that describes the behavior of a software feature well-defined and easy to understand since is described closer to our natural language.
Given, when, then
Is the way we describe a test case using BDD where:
Given: Represents the context in which your behavior will be in. It is the state that your behavior will have before its start.
When: Is the behavior you are testing, describes what is going to happen.
Then: Describes the expected output based on the context(Given) and the behavior (When).
Example:
As we can note on the example above the spec is well-described and simple to understand the feature from every perspective, form the developer, to the tester or the business people.
Quick and Nimble
Now that we already have a good notion about what is BDD, let’s dive in how to do it on our iOS apps using Quick and Nimble \o/
Quick: Quick is a behavior-driven development framework for Swift and Objective-C. Inspired by RSpec, Specta, and Ginkgo.
Nimble: Quick works together with Nimble that is a assertion framework that make it easier and clearer to match the expectations on tests.
You can check out more about the frameworks in the docs [2] and [3].
Let’s see an example
As we can note on the very simple example above, the spec is very readable and you can know just by reading the context what the context is, what is the behavior being tested and the outcome expected.
We can also do async tests using Quick and Nimble. Let’s see the same test above with an async call to return the time of the pitstop.
We can say that the toEventuall
is similar to the XCTest waitForExpectation
API. But it will wait until the condition matches or it reach the timeout.
There are many Open Source projects that use Quick and Nimble in their tests. If you want to take a look and see more examples, you can checkout them here.
Conclusion
Using Quick and Nimble or just the whole BDD concept to write our tests can be useful because it allows us to describe the features or API’s in a very readable and understandable way. From a developer perspective, when you enter a new team and project that you aren’t familiar, looking to the test cases is a good way to understand the features and using the given, when, then concept to describe than can make it simple to reason about the code.
We also have nice API’s especially in Nimble that makes assertions great in describing better our specs and make then more clear and readable for other developers. Even someone like a QA that normally don’t have much experience with code could read the specs a better understanding of the high-level features our the project.
That’s all for this article \o/
If you got some comment or question, please let me know. Your feedback is really important so we can improve this and the future posts and it will be great to receive it :))
Thanks for reading :)