swiftunit-testingdebuggingswiftuiremote-debugging

Why am I getting "No such module 'XCTest'" when I only moved the file back and forth?


Okay so I was recommended to use XCTests, but it has completely baffled me. So I tried to initialize a minor test in it. I have redacted the names of my program because I am unsure of how privacy works online.

// -----Tests.swift

import XCTest 

final class Tests: XCTestCase { 

    func testCallMoveFunctionForPlayer() throws {
        // Create a Board instance
        let board = Board()
    
        // Create a Mob instance
        let mob = Mob()
    
        // Define the start point
        let startPoint = Coord(row: 0, col: 0)
    
        // Place the Mob at the start point on the Board
        board.pieces[startPoint] = mob
    
        // Define the destination
        let destination = Coord(row: 1, col: 1)
    
        // Call the function to test
        board.callMoveFunctionForPlayer(tapRow: destination.row, tapCol: 
        destination.col, startPoint: startPoint, piece: mob)
    
        // Check if the Mob has moved to the destination
        XCTAssertNil(board.pieces[startPoint])
        XCTAssertNotNil(board.pieces[destination])
        XCTAssertEqual(board.pieces[destination], mob)
    }
}

However, I was told that neither Mob nor Board were within the scope. So I moved the file right next to my app file, but it still didn't work, and then I moved it back, but now I have a different issue "No such Module as 'XCTest'" which makes no sense to me, I moved it into the same file it came from.

I was hoping for a simple way to automate checking for some of the most common bugs in my game, but it didn't work. I tried undoing all the way back, but even at its original state, it still claims that the module doesn't exist. I didn't delete any files or anything else, I just moved it back and forth. I changed nothing else.


Solution

  • XCTests are run in a separate app target. This is essentially a separate app. You don't want your test code to be apart of your main application so you can keep it separate and know that it wont accidentally (or adversely) affect anything. This means that you need to expose your main code to the test target. This is done by importing your app using @testable

    Add this to the top of your test file

    import XCTest 
    
    @testable import YourAppName // <--- Add This
    
    final class Tests: XCTestCase { 
    
        func testCallMoveFunctionForPlayer() throws {
            // Test code
    

    This exposes all the code from your main app to your test target, without exposing the test code to your main app. Allowing you to use all the code you have developed in the tests.

    In terms of the No such module as XCTest error, it sounds like you changed the target of your test file. Open the test file in your editor, then in the inspector pane on the right side, under Target Membership, check that it is only apart of your test target, and not a part of your main target.