Cannot enable Unit testing

I have been trying for a couple of days now to get started on building unitests for my Swift 3 IOS project in Xcode.


when ever I try to import a class from my project I get an error "No such module".


I have tried many different steps including the following in multiple differnet combinations and I still have not been able to import my classes from my project into the test frame work. Since my project uses CocoaPods (and there are multiple issues there alone) I tried to isolate the problem with a test project that does not use PODS with no success. I get the same result.


  • enabling "Defines module"
  • setting up a module map file
  • including the module map file in the Swift import paths


I though that maybe my problem might be because I have object derived from NSOBjects, but even with a regular swift class, I cannot get any class defined in my project to be recognized inside the test frame work.


I have been able to get the app recognized with the @testable option on the import line, but no matter what I do I cannot get any class defined in my project to import into the test frame work


Obviously I am doing something wrong? When you create a new swift 3 project, what steps do I need to do to enable testing?


If I have an existing swift 3 project, how do I make sure that unit testing is properly enabled?


I am really confused at this point and I could really use a step by step guide

Replies

When you talk about having problems importing an app class, are you talking about importing a class name at the top of the unit test file, like importing a header file?


import ClassName


Or are you getting errors when you try to access your app classes in your unit tests?


If you are trying to import your class names at the top of the unit test file, stop. You don't need to import individual classes in Swift unit tests. To access your app classes in unit tests in Swift, all you need to do is add the @testable import statement at the start of the unit test file and supply your app name.


import XCTest
@testable import MyApp


If you're still getting errors accessing your app classes in your unit tests, make sure the app name you're using in the @testable import statement (MyApp in the example) matches the value of the Product Module Name build setting. The values might not match if your app name has spaces in it.

All of the above actually.


I get an error on the import directive

I added the "@testable import myApp" to the top of the souce file which initially gave me errors, but at somepoint those errors went away and I don't understand what I did to fix that problem...


Oh, I forgot to add to the original post, I did have the Product module name correctly named.

I'm confused. You say you get an error on the import directive. Then you say adding the @testable import statement made the errors go away. Which is it? Do you have errors or did the errors go away? What is the import directive that you say is giving you an error? Remove every #import ClassName statement in your unit test files.


If you still get errors when you remove all the #import ClassName statements, you'll need to provide more information for anyone to help you. List all the steps you are taking to create your test project. Show the code in your unit test file.


An Internet search for swift 3 unit testing brought up a lot of helpful tutorials on setting up unit testing with Swift. I'd link to them, but Apple puts posts with external links into moderation, and I don't feel like having this reply sit in moderation for days.

I had the same with TestTarget which can't resolve the main target local SPM finally, fix with this approach I added all main target dependencies to the test target too and after that find, I had some compiler issues in which the compiler showed me this wrong error, and when errors were fixed this error disappeared