Testing
Improving test coverage with parameterized tests in Swift testing
Published on: October 31, 2024When you subscribe to the practice of test-driven development or just writing tests in general you'll typically find that you're going to be writing lots and lots of tests for pretty much everything in your codebase. This includes testing that varying inputs on the same function or on the same object result in expected behavior. For example, if you have a function that takes user input and you want to make sure that you validate that a user has not entered a number greater than 100 or smaller than 0, you're going to want to test this function with values...
Read more...Swift Testing basics explained
Published on: October 23, 2024Swift testing is Apple's framework for running unit tests in a modern and more elegant way than it was with XCTest, which came before it. This post is the first one in a series of posts that will help you start using Swift Testing in your projects. In this post, we'll take a look at the following topics: Adding a Swift Testing to an existing project Writing your first Swift test Understanding Swift Testing syntax Let's go ahead and dive right in and see what it takes to add a new Swift test to an existing project. Adding a Swift...
Read more...Testing completion handler APIs with Swift Testing
Published on: October 16, 2024The Swift testing framework is an incredibly useful tool that allows us to write more expressive tests with convenient and modern APIs. This is my first post about Swift Testing, and I’m mainly writing it because I wanted to write about something that I encountered not too long ago when I tried to use Swift testing on a code base where I had both async code as well as older completion handler based code. The async code was very easy to test due to how Swift Testing is designed, and I will be writing more about that in the future....
Read more...Getting started with testing your Combine code
Published on: May 11, 2020A question that often comes up when folks get into learning Combine is "how do I test code that uses Combine?". In this week's post, I will briefly explain the basics of testing Combine code. I will assume that you already know the basics of testing and Combine. If you're just getting started with both topics or would like a refresher I can recommend that you take a look at the following resources: My series of posts on testing My series of posts on Combine My Practical Combine book if you want to learn a lot more about Combine, and...
Read more...Faking network responses in tests
Published on: October 21, 2019Modern applications often rely on data from a network connection to work as intended. Sometimes they rely heavily on the network and are almost worthless without an internet connection while other apps can function mostly fine without a network connection. What these apps have in common is that they contain code that might be challenging to write tests for. Whenever you write unit tests, you should always strive to make your tests as predictable, reproducible and most importantly independent of external factors as possible. This is a huge difference compared to integration testing where you’d test a certain part of...
Read more...Getting started with unit testing your Swift code on iOS – part 2
Published on: October 2, 2019In part 1 of this two-part blog post, you’ve learned how to write synchronous unit tests for a login view model. As a reminder, you saw how to implement tests for the following requirements: When both login fields are empty, pressing the login button should trigger an error that informs the user that both fields are mandatory. When one of the two fields is empty, pressing the login button should trigger an error that informs the user that the empty field is mandatory. When the user’s email address does not contain an @ symbol, pressing the login button should trigger...
Read more...Getting started with unit testing your Swift code on iOS – part 1
Published on: September 30, 2019Recently, I ran a poll on Twitter and discovered that a lot of people are not sure how to get started writing tests, or they struggle to get time approved to write tests for their code. In this blogpost, I will take you through some of the first steps you can take to start writing tests of your own and help you pave the way to a more stable codebase. Why bother with tests at all? You might be wondering why you should bother with code that tests your code. When you put it like that, the idea might indeed...
Read more...Spend less time maintaining your test suite by using the Builder Pattern
Published on: September 16, 2019Often when we write code, we have to initialize objects. Sometimes the object’s initializer doesn’t take any arguments and a simple let object = MyObject() call suffices to create your object, other times things aren’t so simple and you need to supply multiple arguments to an object’s initializer. If you have read my previous post, Cleaning up your dependencies with protocols , you might have refactored your code to use protocol composition to wrap dependencies up into a single object that only exposes what’s needed to the caller. In this blogpost I would like to show you a technique I...
Read more...