Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should a Swift + Objective-C project be setup for Unit Testing

I'm finding it very hard to find out how to configure test targets in Xcode 6b4. Can someone point me in the right direction given this scenario.

I have a mostly Swift project. However, there are some 3rd party Objective-C dependencies, which get put into the application's bridging header. I want to write tests for my Swift code. Ideally, in Swift. The problem I have is this....

  1. If I create a Swift test case, then the compiler complains that it can't find the Objective-C headers in the application's bridging header.

  2. If I create an Objective-C test case, then I cannot import the Swift classes which I want to test.

The only thing which I can do is write Objective-C tests cases, which don't touch any Swift. I cannot write "purely Swift code/tests" due to the Objective-C dependencies.

Does anyone have any advice or had success on this. Or is this the current state of things in Beta 4?

like image 969
Daniel Thorpe Avatar asked Jul 24 '14 11:07

Daniel Thorpe


People also ask

How does Swift implement unit testing?

To create new unit case in iOS, go to File -> New -> File, and then select Unit Test Case Class. Doing so creates a template just like the one you got with your project. In our case, we want to name the file to correspond with the new Pokemon-related data structures we have introduced.

How use Objective-C and Swift together?

To import a set of Objective-C files into Swift code within the same app target, you rely on an Objective-C bridging header file to expose those files to Swift. Xcode offers to create this header when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

Can we use Swift files in Objective-C project?

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code.


1 Answers

I am not sure if you are still looking for this as Xcode 6 is not in beta any more, but you can add your Objective-C Bridging Header file to your Unit Test Target in the target settings. This fixed the issue for me.

Objective-C Bridging Header Xcode settings

like image 59
Barlow Tucker Avatar answered Oct 19 '22 05:10

Barlow Tucker