---
title: Testing and performance
framework: Technology Overviews
role: article
path: technologyoverviews/testing-and-performance
---

# Testing and performance

Discover and fix potential issues in your code by testing and regularly collecting performance metrics.

## Create a test plan for your project

Testing your code and analyzing its performance throughout the development process can take your project to the next level. A comprehensive set of tests helps you verify your code runs the way you expect, and performance metrics help you determine if your app uses resources efficiently. When you regularly gather test and performance data, you become aware of problems early, and have time to fix them. Create a test plan for your project A Improving code assessment by organizing tests into test plans is an essential part of the development process for teams of all sizes. A test plan comprises several elements: Write unit tests for your code that validate individual behaviors of a type or feature. Test interactions with your app’s interface that validate how your app responds to direct interactions with its interface. Test suites, or groups of tests, that you run together to validate a particular type or feature. Test bundles, or collections of test suites, that you run to validate your entire codebase. In Xcode, Adding tests to your Xcode project to your project and use it to create your initial tests and test suites. Each test bundle has an associated target that you build and run before you run the tests. You can run your tests every time you build a target, or run them only at specific times. For example, you might run tests only before you submit changes to your source control system. The Test navigator pane in your Xcode project displays the overall test plan for your project, and the tests from all your test bundles. Running tests and interpreting results from Xcode or the command line, and verify the results before submitting any code changes to the project. Automate your tests by running your test plan in Xcode Cloud. Write unit tests for your code A unit test is a function that runs some of your code and determines whether that code delivered the expected results. You can create any number of unit tests for your app and use them to validate the behavior of specific types or features. For example, one test might verify that a custom object adds data correctly, while a second test verifies the removal process. To write unit tests for your code, Adding tests to your Xcode project to your project and configure it to use the Swift Testing or XCTest framework. Both frameworks provide code-level support for writing unit test functions and checking the expected results. Swift Testing provides powerful and expressive tools to declare and manage your unit tests, and it’s a great choice for testing your Swift code. Use XCTest for any UI tests you create, and for code you write using Swift, Objective-C, and other C-based languages. To test your app’s In-App Purchase code, include the StoreKit Test framework in addition to one of the other frameworks. The following listing shows the same unit test in Swift Testing and XCTest. Swift Testing uses a macro-based approach to Defining test functions, leading to short and easy-to-read test code. Create dedicated XCTest types and use them to Defining Test Cases and Test Methods. View your project’s unit tests in the Test navigator pane of the Xcode project window. Use this pane to run individual unit tests or groups of unit tests at any time. Alternatively, run tests directly from the source window that contains your test code. To collect performance metrics for your tests, run them in Instruments. When deciding what tests to create, include a mixture of tests with both positive and negative outcomes. It’s important to verify your code handles data correctly, but it’s also important to verify your app handles boundary conditions or bad data correctly. For example, you might deliberately run a test with bad data to verify your code returns an appropriate error. Test interactions with your app’s interface UI tests verify that the code for your app’s interface delivers expected results. The tests simulate direct interactions with your app’s interface, and capture the results for you to examine. Like unit tests, you use UI tests to test specific workflows in your app. For example, a test might open a data entry form, populate the fields with specific values, and verify your code handles the data correctly. Create UI tests inside Adding tests to your Xcode project and write them using XCTest and the XCUIAutomation framework. XCTest provides the types you use to create your tests, and XCUIAutomation works with your app’s Accessibility to give you references to views and other elements in your interface. Xcode offers a way to Recording UI automation for testing, and turn them into the code for a UI test function. After you record a set of interactions, augment the generated code to check values or the state of your app. Rewrite the transcribed UI interactions as needed to make your tests more robust. In your test plan, run your UI tests on a variety of devices and languages your app supports. Different configurations help you identify problems you might not have anticipated. For example, testing on different devices show you places where your UI doesn’t adjust properly. Similarly, testing in different languages can uncover Localization issues. Adopt a continuous integration and delivery strategy To catch errors as early as possible, run tests regularly and analyze the results. About continuous integration and delivery with Xcode Cloud is a continuous integration and delivery (CI/CD) system that integrates with Xcode, TestFlight, and App Store Connect. Use it to Create practical workflows in Xcode Cloud that build your project and run tests automatically in iCloud. For example, a workflow might build and run your tests each time a developer merges a pull request. You can specify different devices and languages to use during testing, and you can even run custom scripts to handle project-specific actions. For UI tests, you can configure Xcode Cloud to capture videos of each test as it runs. If a particular test fails, use these videos as a first step to diagnose the failure. Use the other information that Xcode Cloud collects to inspect your UI and locate where a particular test failed. Gather and analyze performance metrics Efficiency is about maximizing the amount of work your app performs while minimizing its use of memory, battery, and other system resources. Improving your code’s efficiency can help it run faster, use less memory, and use less energy. These adjustments help improve the overall experience people have with your app. Instruments Improving your app’s performance about what your code is doing and what resources it’s using. After you collect a set of baseline performance metrics, capture new sets periodically to determine if performance improved or diminished. Use Instruments to collect data about: The time it takes your app to Reducing your app’s launch time. The amount of Reducing your app’s memory use, and how it uses that memory. How often Understanding and improving SwiftUI performance update their contents. Places where your code Addressing CPU bottlenecks or runs code inefficiently. How much time your app spends Improving app responsiveness waiting for files, threads, network data, or other resources. Where your app’s graphics code experiences Understanding hitches in your app or Understanding hangs in your app. How much Reducing your app’s battery use when it runs. The efficiency of your app’s concurrent tasks. You can run Instruments with the same tests you create to validate your app’s behavior, or you can build custom tests to collect performance metrics for specific features. Sample your code to identify potential problems, and switch to a Analyzing CPU usage with the Processor Trace instrument as needed to see the precise set of branches your code takes when running in the CPU.
