How to add the LanguageCoordinator to Swift Testing

Oscar de la Hera Gomez
Three flowers that represent Swift, Swift Testing and XCode side by side. Beneath them sits the text "Language Coordinator".

Add the targets to the files, modify the initializer and generator. Then initialize the LanguageCoordinator with a isTestingEnvironment flag in the tests.

The following tutorial can be found in the main branch of our Open Source Swift and SwiftUI projects linked below.

Step One: Add the Language Coordinator

A screenshot of XCode showing the SampleScreen code for this tutorial.

If you haven't already, add the Language Coordinator and relevant files to the project by following one of the two tutorials below.

Step Two: Add the Testing Targets to All Files

A screenshot of XCode showing the files we selected and how you can add a target using the + button on the inspector on the right hand side.

Select all the LanguageCoordinator, UIContent, Language and String files and add the testing targets to them.

Please note that we have also added targets for our debugging identifiers.

Make sure you add all relevant testing targets.

A screenshot of XCode showing the modal that allows you to add targets. We have highlighted the test targets and the save button.

Step Four: Update the Initializer

A screenshot of XCode showing the LanguageCoordinator.swift file. Highlighted is how we have added a isTestingEnvironment boolean constant, how it is offered as an optional in the initializer and how its initialized within the initializer with a default of false.

Add an isTestEnvironment boolean constant, and within the initializer set it as an optional parameter that defaults to false.

Please note that we did not use a launch argument as the app must launch with the standard bundle path, whilst the test requires a different path. 

If you use a launch argument, both will use the different path, which will cause the App to not find the files - resulting in empty strings.

Step Five: Update the Generator

A screenshot of XCode showing the LanguageCoordinator+Generate.swift file. Highlighted is how we have adapted the path for the string json files to take in different values if its within the testing environment.

Update the LanguageCoordinator+Generate.swift file to use a different path if it uses the testing environment.

Step Six: Initialize the LanguageCoordinator

A screenshot of XCode showing how we have initialized the LanguageCoordinator in the test files. The initializers set the isTestingEnvironment to true in the initializer.

In the UI Testing or Unit Testing swift file, initialize the LanguageCoordinator and in the initializer set the isTestEnvironment variable to true.

Step Seven: Test

A screenshot of a UITest that has successfully passed.

Write a test that verifies that the LanguageCoordinator text is working within the UI Test or Unit Test.

Run it and verify it works.

Looking to learn more about SwiftUI, Swift, Swift Testing, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, Swift Testing, design and technology.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details