Testing is an integral part of iOS app development.
XCTest is the built-in testing framework provided by Apple for writing and running unit tests for your iOS apps.
In this beginner's tutorial, you'll learn how to get started with XCTest and write your first set of unit tests for an iOS app.
First Step Create a New iOS Project with Unit Test
If you don't have an existing iOS project to work with, let's create a simple one to get started.
- Open Xcode and click on "Create a new Xcode project."
- Choose the "Single View App" template and click "Next."
- Fill in the project details, including the product name, organization name, and bundle identifier. You can leave other settings as their default values. Click "Next."
- Choose a location to save your project and click "Create."
Create a Function inside your Unit Test
As this tutorial aim to enable you to test production code using unit test, you will need to create a function to test in main app.
Start by adding
func stringSplit
in ViewController.swift
file.class ViewController: UIViewController { // .. other code func stringSplit(input: String) -> [String] { return input.split(by: " ") } }
The
stringSplit
method will create array for each word separated by " "
.Unit Testing the stringSplit
Function
Now, let's proceed with writing your first XCTest for this function:
- In the Project Navigator on the left, locate the "Tests" folder. If it's not visible, right-click on your project name and choose "New Group." Name it "Tests."
- Right-click on the "Tests" folder and select "New Unit Test Target."
- In the dialog that appears, ensure your app target is selected and click "Finish."
- Xcode will create a new test target with a file named
YourProjectNameTests.swift
, whereYourProjectName
is the name of your project. Open this file.
- You'll see a template for a test class and a test method. Modify the code as follows:
swiftCopy code import XCTest @testable import YourProjectName // Import your app's module class YourProjectNameTests: XCTestCase { func test_stringSplit() { // Arrange let viewController = ViewController() let input = "Hello World" // Act let result = viewController.stringSplit(input: input) // Assert XCTAssertEqual(result, ["Hello", "World"], "String should be split into an array of words") } }
In this updated code:
- We import your app's module using
@testable import YourProjectName
. Make sure to replaceYourProjectName
with the actual name of your app.
- We create a test method
test_stringSplit
that tests thestringSplit
function.
- In the test method, we arrange the necessary data, which includes creating an instance of
ViewController
and providing an input string.
- We then act by calling the
stringSplit
function with the input and store the result.
- Finally, we use
XCTAssertEqual
to assert that the result is equal to the expected output, which is an array of words extracted from the input string.
In the "Product" menu, select "Test" to run your tests. You should see a message indicating that the test has passed successfully. This confirms that your
stringSplit
function is working as expected.Writing More Tests
Now that you've written your first XCTest, you can add more tests to cover different parts of your code. Here are a few tips:
- Create separate test methods for different functionalities.
- Use XCTest assertion methods like
XCTAssertEqual
,XCTAssertTrue
,XCTAssertFalse
, etc., to check if your code behaves as expected.
- Test edge cases and error scenarios to ensure your app handles them gracefully.
- Organize your tests into logical groups within the test class.
Conclusion
XCTest is a powerful tool for testing your iOS apps, helping you catch bugs and ensure the correctness of your code. As you continue developing your app, consider writing unit tests for critical components and features to maintain code quality and reliability. XCTest provides a robust foundation for building a comprehensive test suite for your iOS projects.