Is the testing framework broke in linux swift 5.1?

  1. The default test works, but that is because it opens the program in a pipe and reads STDOUT.
  2. Any time I attempt to run a function declared in the main file, I get "error: undefined reference to '$s4temp5HelloSSyF'" <- Any function I attempt to run

I'm not a compiler person, but I believe there is an issue with the symbol naming or name mangling (whatever it is called). Or, I am just doing it all completely wrong, quite possible. I'd appreciate any help.

My test file:

    import XCTest
    import temp
    final class tempTests: XCTestCase {
        func testExample() throws {
            // This is an example of a functional test case.
            // Use XCTAssert and related functions to verify your tests produce the correct
            // results.

            // Some of the APIs that we use below are available in macOS 10.13 and above.
            guard #available(macOS 10.13, *) else {
                return
            }
            let item = Hello()
            XCTAssertEqual(item, "Hello, world!\n")
        }

        static var allTests = [
            ("testExample", testExample),
        ]
    }

main file:

            public func Hello() -> String {
                return "Hello, world!"
            }

edit: I have also tried changing the function name to lowercase and using the @testable import, but it is the same error. The program I am attempting to test is much bigger, but I took it down to the barest form and I am getting the same issue, which is why I am wondering if maybe the testing suite is broke in 5.1

edit2: Adding my LinuxMain.swift and XCTestManifests.swift in case it helps:

LinuxMain:

            import XCTest

            import tempTests
            var tests = [XCTestCaseEntry]()

            tests += tempTests.allTests()
            XCTMain(tests)

XCTestManifest:

            import XCTest

            #if !canImport(ObjectiveC)
            public func allTests() -> [XCTestCaseEntry] {
                return [
                    testCase(tempTests.allTests),
                ]
            }
            #endif

XCTest doesn't support testing executable modules. You need to extract the Hello function into a library module, and import that module in your main.swift to use it.

ohhhhh I will do that, thanks!

Terms of Service

Privacy Policy

Cookie Policy