19

I have recently converted my app, and unit tests over to Swift 2.0. I am using @testable import AppName in my Tests.swift files.

Due to an issue with not being able to execute subclasses in multiple targets (see here for the issue), my class EntityName can only be of Target Membership AppName, and NOT AppNameTests.

The problem is, once I switch over to the AppNameTests schema to run unit tests, code in the module AppName can't find the class EntityName and I get

Use of undeclared type 'EntityName'

How do I get AppName to compile when running tests from the AppNameTests scheme without that entity class not a member of the scheme?

5 Answers 5

20

Due to an issue with not being able to execute subclasses in multiple targets

When compiling the same Swift code as part of different targets, the compiler actually generates different classes. So this behaves as designed, but is almost certainly not what you want when running an app's unit tests.

There are two ways I'd recommend you set up your models to allow testing:

1. Public models (recommended)

In your app target:

import RealmSwift
public class MyModel: Object {}

This code should only be compiled as part of your application target, and your unit tests can be set up in the following way:

import MyApp
// should be able to access `MyModel`

Easy enough?

2. @Testable (Swift 2 only)

This approach relies on the @testable keyword introduced in Swift 2.

In your app target:

import RealmSwift
internal class MyModel: Object {} // ACL cannot be `private`

Again, this code should only be compiled as part of your application target, and your unit tests can be set up in the following way:

@testable import MyApp
// should be able to access `MyModel`

Make sure MyApp's build settings have Enable Testability set to YES.

This approach may be preferred to public models if you're building a framework where some internal models shouldn't be accessible from users of that framework.

Realm has a dedicated section of their documentation detailing these common approaches to testing which you can read here: https://realm.io/docs/swift/latest/#avoid-linking-realm-and-tested-code-in-test-targets

5
  • right, i can access MyModel from my tests using @testable, but files that access my model from within MyApp cannot when compiling MyAppTests! very frustrating
    – Tyler
    Aug 28, 2015 at 18:15
  • sounds like something's wrong with your project's configuration. If I had to guess, I'd say the model file is being compiled with the unit test target but not the app target. I'd be happy to look into it if you send your project to help@realm.io.
    – jpsim
    Aug 28, 2015 at 18:40
  • 1
    hey, turns out i had to stop targeting Tests for my entire MyApp module.. nuts, not it works!
    – Tyler
    Aug 28, 2015 at 20:19
  • 1
    Also, for @testable import to work, the test target must be a "Unit Testing Bundle" (not a UI Testing Bundle).
    – Rolleric
    Jan 27, 2021 at 22:02
  • what if i would only like to import one viper folder for unit testing, what are the build performances on @testable import myApp which includes the entire app? I dont like waiting for kiwi tests on xcode May 17, 2021 at 0:22
10

I had to stop targeting my entire apps .swift files to be have membership of MyAppTests, and rely solely on @testable import MyApp

2
  • That's exactly how it's supposed to work. Only compile things once!
    – jpsim
    Aug 28, 2015 at 23:54
  • 4
    Note MyApp must be the product module name in your project (Settings -> Target -> Build Setting -> Product Module Name)
    – Pablo A.
    Nov 17, 2016 at 17:39
0

I also got this error recently and none of the above steps fixed the problem, what did fix it was removing non-swift file from the Compile sources build phase in the Target you want to run tests on. This was failing silently

enter image description here

0

First ensure that @testable import MyApp is included in every test file. Then, in your Test Target Build Phases, remove all non-test files the Copy Bundle Resources sections. App files that are in your test target that point to app files that are not in your test target are breaking your unit tests. Remove all app files from your test target and add the @testable flag and everything should work!

0

I had similar problem and the issue was that all of the application source files were linked to the unit test target, and so compiled twice!

Also, I installed Realm via Carthage and had to include both targets (main and test) for the frameworks "Realm.framework" and "RealmSwift.framework" because it wouldn't work otherwise.

Here is the issue link

https://github.com/realm/realm-cocoa/issues/3627

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.