Swift test give error “Undefined symbols for architecture x86_64”

前端 未结 7 829
借酒劲吻你
借酒劲吻你 2020-12-24 06:58

I\'m running swift test from the command line to run the test cases. This is the test case:

import XCTest
@testable import vnk_swift

class KeyM         


        
7条回答
  •  甜味超标
    2020-12-24 07:28

    This seems to be caused my having a main.swift in your module directory. This results in an executable being build, instead of a library against which your test cases can be linked.

    I resolved this issue by splitting my code into two modules. A library for which I have test cases and the application that only contains main.swift:

    Package.swift
    Sources\FooBarLib\
    Sources\FooBarLib\Something.swift
    Sources\FooBarLib\MoreStuff.swift
    Sources\FooBarApp\main.swift
    Tests\FooBarLibTests\TestSomething.swift
    

    Then in my Package.swift make sure FooBarApp depends on FooBarLib:

    import PackageDescription
    
    let package = Package(
        name: "FooBar",
        targets: [
            .target(name: "FooBarLib"),
            .target(name: "FooBarApp", dependencies: ["FooBarLib"])
        ],
    )
    

    Then in TestSomething.swift you import the FooBarLib module:

    @testable import FooBarLib
    import XCTest
    
    class TestSomething: XCTestCase {
        func testFunc() {
        }
    }
    

提交回复
热议问题