Home > other >  Why can't I test a basic Multiplatform app with Xcode?
Why can't I test a basic Multiplatform app with Xcode?

Time:04-23

I created the new Multiplatform app with Xcode called Tester and then added a super simple class:

class Transaction {
    var time = "1"
}

In the Tests_iOS I have this:

import XCTest
@testable import Tester

class Tests_iOS: XCTestCase {
    func testExample() throws {
        let t = Transaction()
        XCTAssertEqual(t.time, "1")
    }
}

The app compiles, however when I try to run the tests, compilation fails with this error:

Undefined symbols for architecture arm64:
  "type metadata accessor for Tester.Transaction", referenced from:
      Tests_iOS.Tests_iOS.testExample() throws -> () in Tests_iOS.o
  "Tester.Transaction.__allocating_init() -> Tester.Transaction", referenced from:
      Tests_iOS.Tests_iOS.testExample() throws -> () in Tests_iOS.o
ld: symbol(s) not found for architecture arm64

What am I doing wrong? This is on an M1 Mac using Xcode 13.3

CodePudding user response:

The problem is most likely that you have added your test to the test target that gets automatically created when you create a new multi-platform project and that target is a UI test target and not an ordinary unit test target.

For those tests you do not access the public api of your app like your Transaction class but instead UI components so the test framework has no access to the Transaction class.

The solution is to create a new unit test target, File -> New -> Target... and select Unit Test (remember to select the correct platform because this needs to be done separately for each supported platform).

Once done, move your test to the new target or copy it to the test file that is generated and your test will build and run.

  • Related