xcodeunit-testingxctest

How to link a small Xcode unit test with undefined symbols


In an attempt to implement a solution for this problem, I have Swift source file zShearFactor.swift that defines a function zWarpShearFactor that returns a struct of type SVDFactors:

import Foundation
import simd

struct SVDFactors {
    let U : simd_float3x3
    let S : simd_float3x3
    let V : simd_float3x3
}

func zWarpShearFactor(a : Float, b : Float, c : Float) -> SVDFactors {
   ...
}

Trying to be the diligent programmer I ought to be I created a XCode unit test

import XCTest
import simd
@testable import zshear_factor

class zshear_factor_test: XCTestCase {
    static func verifySVD(a : Float, b : Float, c : Float, svd : SVDFactors) -> Bool {
        let A = simd_float3x3(rows: [
            simd_float3(1, 0, a),
            simd_float3(0, 1, b),
            simd_float3(0, 0, c)
        ])
        let A_ = svd.U * svd.S * simd_transpose(svd.V)
        return simd_almost_equal_elements(A, A_, 0.0001)
    }

    func testzWarpShearFactor() {
        let testValues : [Float] = [0, -1, +1, 10, -10, 1.23, -1.23]
        for a in testValues {
            for b in testValues {
                for c in testValues {
                    let SVD = zWarpShearFactor(a: a, b: b, c: c)
                    XCTAssertTrue(Self.verifySVD(a: a, b: b, c: c,
                                                 svd: SVD),
                                  "zWarpShearFactor(\(a),\(b),\(c) FAIL")
                }
            }
        }
    }
}

Even though I imported the via @testable import zshear_factor there is obviously more I need to do since I get a linker error when I click on the diamond to run the Xcode project:

enter image description here

The initial question is what do I put in the Build Settings so that the proper code is linked with the unit test? The bigger question is how is this done on very large projects when there is just a tiny function in small file you want to test? The @testable import approach seems to include everything in the app including the the kitchen sink.


Solution

  • In your Sample GitHub project, you have the following errors:

    @testable import zshear_factor //"No such module 'zshear_factor'"
    

    So remove it.

    Later you have Cannot find 'zWarpShearFactor' in scope, so zWarpShearFactor(a:b:c:) is unknown. That's because zShearFactor.swift isn't in the "Unit Test App", it's only in the "Main App".

    enter image description here

    So add it to the "test app", by checking the box "zshear-factor-test" and you're good to run.