XCode Unit Tests - Cannot find viewcontroller in scope

20.1k views Asked by At

At some point in my app's development, my Unit Tests and UI Tests lost access to my app's main module:

Cannot find type 'FileVacuumViewController' in scope

Cannot find type 'FileVacuumViewController' in scope

They worked with the xctestplan before. I don't know at what point it happened. I tried not importing @testable, including the tests.swift in the main module's "Target Membership".

Maybe I need to open the workspace instead of the xcproject? When I try to open the workspace there was an error message and no files show in the tree: enter image description here


Here's my xctestplan config:

xctestplan config

When I clicked to run one the UI tests individually, about 20 of these code sign dialogs showed:

code sign popups

4

There are 4 answers

2
manman On BEST ANSWER

Looks like your test file is included in both your app target and test bundle. Make sure to uncheck it from app target and try clean & build.

1
Vladimir Sukanica On

Select file "FileVacuumViewController" in Project Navigator and, in File Inspector, in the section Target Membership, check field which ends with Tests and UITests. Clean and build project, if necessary.

0
Paul Lehn On

I just ran into this issue myself after upgrading to Xcode 13.

The solution for me was to remove the file from both XCTest AND XCUITest Target dependencies:

Project Settings -> <Target>Tests -> Build Phases -> select file and click "-"
Project Settings -> <Target>UITests -> Build Phases -> select file and click "-"

Now try to run your test and add @testable import <Target> to the top of your XCTest files that error out. NOTE: you should not have to add this at all to your UITests files at all.

1
Aaron Breckenridge On

I ran into this recently with XCode 13 while following a blog post on creating unit tests. For some reason, XCode didn't automatically add the ViewController to the Test target.

My solution was to make sure that the ViewController was included in the Target Membership for the test suite. I had to also make the same Target Membership change for the Main storyboard.

Screenshot of selected checkbox for the project's tests in the Target Membership section of XCode while the View Controller is selected in the Files pane