Reputation: 2348
I try to add Unit tests to my Swift project on Linux Ubuntu 16.04. Now I have the directory structure:
MyProject
|-Sources
| └MyProject
| |-IPcalc.swift
| └ main.swift
|
|-Tests
| |-MyProjectTests
| | └IPcalcTests.swift
| └ LinuxMain.swift
|
└ Package.swift
IPcalcTests.swift file:
import XCTest
@testable import IPcalc
...
LinuxMain.swift file:
import XCTest
@testable import IPcalcTests
XCTMain([
testCase(IPcalcTests.allTests),
])
Package.swift file:
import PackageDescription
let package = Package(
name: "MyProject",
products: [],
dependencies: [],
targets: [
.target(
name: "MyProject",
dependencies: []),
.testTarget(
name: "MyProjectTests",
dependencies: ["MyProject"]),
]
)
When I try to execute swift test , I get:
$ swift test
Compile Swift Module 'MyProjectTests' (2 sources)
/home/user/MyProject/Tests/MyProjectTests/IPcalcTests.swift:2:18: error: no such module 'IPcalc'
@testable import IPcalc
Why I could not import IPcalc class to IPcalcTests.swift?
Upvotes: 1
Views: 472
Reputation: 2348
Issue happens because my app is executable, it has main.swift file. Unit Test don't work in this case, or I don't know how to use it. It is necessary to split project into projectApp target and projectLib target. projectApp will contain only main.swift and projectLib will contain the rest.
After that Unit Test works without any problems with projectLib target. More details about is here: https://riis.com/blog/swift-unit-testing-ubuntu/
https://www.raywenderlich.com/1072244-server-side-swift-testing-on-linux
Upvotes: 1