Posted on 29 Jun 2020 . 2 min read
Unit testing is a testing method where you can test “unit” of code whether it is working as you want or not. In Xcode, use XCTest framework to perform unit tests.
Unit test is a function starts with lowercase word "test” and it must be method of subclass of XCTestCase . It has conditions to check code is doing right as expected, but it has no parameters and no return value.
Because units tests are perform under unit testing target you must have to add them before use. You can include “Unit Testing Bundle” in Xcode project two ways :
After setup it will generate new subclass of XCTestCase in your project inside testing folder which you can find inside Project navigator.
import XCTest
class XCArticleTests: XCTestCase {
override func setUp() {
// method is called before each test method
// setup code here
}
override func tearDown() {
// method is called after each test method in the
// code to perform cleanup
}
func testExample() {
// add test case.
// Use XCTAssert to test code
}
}This example defines XCArticleTests which is a subclass of XCTestCase . It has three methods setUp() for initial setup, tearDown() to perform cleanup after execution and test method called testExample to perform all tests. If you want to read more about setup and teardown methods go to set up and tear down state in your tests article
Define new extension of type Int with a function called cubed which returns cube number.
extension Int {
func cubed() -> Int {
return self * self * self
}
}
Define new XCTestCase subclass CubeNumberTests with a method named testCubeNumber(). This method creates two properties one is number and another for cubeNumber and checks cubeNumber equals to 125.
class CubeNumberTests: XCTestCase {
func testCubeNumber() {
let number = 5
let cubeNumber = number.cubed()
XCTAssertEqual(cubeNumber, 125)
}
}
Click on gray diamond button on the left side next to test method. The diamond turns into green if test passes or otherwise it will give error message. The above test will be succeed because cube number of 5 matches 125.
Unit testing is a necessary skill if you want to be a good developer. It look hard in the beginning but it gets easier as you use them. Don’t use unit tests all over your application it will be confusing and time wasting .You should use them when it’s too necessary.
Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.
Thanks!
Written By

Gurjit Singh
I’m Computer Science graduate and CompTIA Security+ certified SOC Analyst and Mobile Application Security Engineer with 10+ years of cross-platform development experience across iOS, Android, and web.
Discover articles by topics
SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView
Static analysis of an iOS app using MobSF, identifying credential misuse, security misconfigurations, and privacy issues....
2026-05-29 . 4 min read MobSF SAST
Transitioning from software development into cybersecurity is one of the most natural career moves in tech today....
2026-05-26 . 4 min read Security Certificates
Swift 6.1, officially released in March 2025, continues the evolution of Apple's powerful and expressive programming language....
2025-08-12 . 3 min read Swift 6.1
In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......
2024-10-17 . 3 min read String Concatenation
With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....
2024-07-09 . 3 min read UIHostingController
In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...
2024-01-28 . 4 min read Swift Autorelease