In Xcode 11, Apple introduced Test Plans, which is a way to group both Unit & UI tests to perform. Prior to Test Plans you could only select to run a single test, a test class or all of the tests, and this wasn’t ideal. There was always a need to run tests with various configurations, schemes or environments.
You can now do this better by introducing Test Plans into your project.
What is a Test Plan
Under the hood, a Test Plan is a JSON file with the
.xctestplan extension and in this tutorial you’re going to create a brand new test plan for your project.
A test plan contains 3 main parts:
- Test targets: Your test plan can contain multiple testing targets, it’s common to have both UI and Unit targets associated to a single test plan.
- Default options: This is a similar shared options that you would find for your scheme or project settings. Here you can override a shared set of options like launch arguments.
- Configurations: This set of configuration is what overrides the shared configurations and is run for each test plan.
Why Introduce a Test Plan?
You’re likely at some point want to organize your tests so it’s efficient for your development workflow. For example, let’s say your project has 200,000 lines of Swift code and you have 90% test coverage. You’re likely going to have 1000’s of tests and it’s going to be very expensive to run these everytime.
Utilising a test plan means that you can organize tests based on priority and also group them based on important parts of your app. For example, you might want to group them Bronze, Silver & Gold. Gold tests running every time but only Bronze & Silver running on a release cycle.
Open your iOS project and make sure you have a test target already created.
Navigate to File -> New -> File…
Next, search for Test Plan and hit create.
You should call this something like “TestPlan” or similar.
Xcode will navigate to your newly created test plan.
Let’s go ahead and add your test target. Tap on the + icon
Select your Test Target and click on “Add”.
Congratulations, you now have a test plan associated to your project.
Xcode will automatically associate this test plan to your scheme, so running your tests now will automatically execute this test plan.
You can enable tests by using the checkmark and you can also go ahead and group the various tests required.
Converting from Schemes
If this is your existing application, Xcode has a built-in feature so you can easily convert your scheme to start using Test Plans.
Navigate to Product -> Scheme -> Convert Scheme to use Test Plans
This will convert your existing configurations to start using test plans.
It’s also possible to have many test plans with different configurations and many test plans can be associated with the same scheme.