I use Core Data's automatically generated classes. My project has 3 targets in addition to the test target. For each target, the Core Data classes are properly generated which I verified by inspecting the Derived Data folder. However, classes are not generated for the Test Target despite it being ticked in the Core Data model file. This causes an "undeclared identifier" and "Use of undeclared type" errors when I try to reference one of the Core Data classes in the test target. How can I fix this please?
7 Answers
You do not need extra classes generated for each test target - your import process should import everything, and no files should need to be added to other targets.
Declaring @testable import MyProject
should take care of everything.
In Objective C
@import MyProject;

- 6,527
- 1
- 29
- 44

- 5,168
- 3
- 29
- 36
-
http://stackoverflow.com/questions/21911168/when-do-app-sources-need-to-be-included-in-test-targets – sschale Apr 01 '17 at 03:34
-
Updated with Objective C version of import. – Dave Weston Apr 01 '17 at 10:03
-
@DaveWeston: '@import' is intended for frameworks, not for targets. If MyProject is a target, then '@import MyProject;' will result in the error "Module MyProject not found." The question that sschale links to is from 2014 and the answers discuss build settings for Xcode 5. – Elise van Looij Nov 02 '18 at 22:01
In Xcode 9.1 try adding your .xcdatamodel
to a test target too. All auto-generated class will be imported too.

- 5,640
- 3
- 58
- 47
-
5Beware of this because, while it allows the test target to compile, you end up with duplicate definitions of your model types. For me, it causes runtime errors when fetching entities because `AppTarget.SomeModelClass` is not the same type as `TestTarget.SomeModelClass`. – ApeOnFire Dec 02 '17 at 12:01
This was due to a bug currently in Xcode (8.3.1) where auto-generated NSManagedObject classes (codegen set to "Class Definition") cannot be found on the global path despite the project compiling successfully. The only way around it is to which to manual generation of the NSManagedObject classes by setting codegen for each entity to "Manual/None".

- 20,288
- 21
- 96
- 151
Select the test target, navigate to Build Settings and search for the setting "Header Search Paths"
Then add the following entry:
$CONFIGURATION_TEMP_DIR/{Project Target Name}.build/DerivedSources/CoreDataGenerated/{Project Name}
Replace the curly brackets with your main target name (not the test target), and your project name, respectively.
Xcode should now be able to find the generated source files when building the test target.

- 521
- 7
- 11
I noticed in Xcode 9.1 that the Data Model Inspector has a drop down for the Module to use. Selecting 'Current Product Module' with the Class Definition Codegen, and including the model in your Test target, compiles without errors. From what I can tell, the problem pieSquared noticed doesn't appear to be an issue, but my tests aren't exhaustive yet. It may be something to try, nonetheless.

- 23
- 1
- 5
I have wrestled with this issue ever since Xcode 9.4 or thereabouts. The error was always the same:
Testing cancelled because build failed. 'MyEntity+CoreDataProperties.h' file not found
I've filed a bug report (45802900), but I got Apple Support involved as well and together we finally found the solution. . Actually, there are two solutions.
Solution 1: Set the Header Search Paths build setting of the Test Target
The most elegant solution, to my mind, is to set the Header Search Paths build setting of the test target. Ziqiao Chen of Technical Support figured out the correct path, while I provided the build variables. For projects with only one data model, the name of which is the same as the project (which is the default), the Header Search Path may be:
$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources/CoreDataGenerated/$(PROJECT_NAME)
Make sure the path is set to 'non-recursive'.
For projects with multiple data models, a non-recursive path should be added to Header Search Paths for each data model:
$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources/CoreDataGenerated/dataModel1 $(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources/CoreDataGenerated/data_model_B
Solution 2: Add the data model to the Test Target
Another solution, which Ziqiao Chen came up with and which I've also read on here on SO, is to add the data model to the test target. In my experience, however, this only works with a single data model. As Ziqiao Chen pointed out, Xcode should generated the exact same files for the test target as for the main target.
My experience is that in more complicated projects (multiple targets, multiple data models) all kind of linker errors may occur, from complaints about duplicates to the "testing cancelled" error described above. For simple projects, however, it's a quick and simple solution.

- 4,162
- 3
- 29
- 52
For Xcode 11.5: if Codegen property is class Definition, and if you are not getting a suggestion for the entity you created in xcdatamodel. Try to quit Xcode and reopen your project again. It works for me. This answer is only if you are not getting suggestion but if your file doesn't get generated try any above answer.

- 61
- 1
- 8