title: How to test a class in a Swift package?
body: <p>I'm currently trying to understand the mechanism of importing dependencies in a Swift package and I've run into an issue with tests. Hope someone can explain what I'm doing wrong. I'm going to describe the problem step-by-step so that you could easily reproduce it.</p>
  
  <p>So I'm creating a new Swift package using <code>swift package init --type executable</code>. This command creates the basic Swift package structure:</p>
  
  <pre class="lang-none prettyprint-override"><code>Artems-MacBook-Pro:SwiftExample artem$ swift package init --type executable
  Creating executable package: SwiftExample
  Creating Package.swift
  Creating README.md
  Creating .gitignore
  Creating Sources/
  Creating Sources/SwiftExample/main.swift
  Creating Tests/
  Creating Tests/LinuxMain.swift
  Creating Tests/SwiftExampleTests/
  Creating Tests/SwiftExampleTests/SwiftExampleTests.swift
  Creating Tests/SwiftExampleTests/XCTestManifests.swift
  </code></pre>
  
  <p>The package itself is called <code>SwiftExample</code>. As you can see the command also creates an example of a unit test case (<code>SwiftExampleTests.swift</code>).</p>
  
  <p>Then I create a simple class called <code>Car.swift</code> and put it into the <code>Sources/SwiftExample/Classes/</code> directory:</p>
  
  <pre class="lang-swift prettyprint-override"><code>// Sources/SwiftExample/Classes/Car.swift
  class Car {
      init() {
          print("I'm a car!")
      }
  }
  </code></pre>
  
  <p>In the <code>main.swift</code> file I can create an instance of the Car class and everything works pretty much fine:</p>
  
  <pre class="lang-swift prettyprint-override"><code>// Sources/SwiftExample/main.swift
  print("Hello, world!")
  
  let car = Car()
  
  </code></pre>
  
  <p>The output would be:</p>
  
  <pre class="lang-none prettyprint-override"><code>Hello, world!
  I'm a car!
  </code></pre>
  
  <p>But the problem is I cannot use this class in my test file. For example, I'm trying to create an instance of the <code>Car</code> class in the <code>testExample()</code> function of the <code>SwiftExampleTests.swift</code> file:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import XCTest
  import class Foundation.Bundle
  
  @testable import SwiftExample
  
  final class SwiftExampleTests: XCTestCase {
      func testExample() throws {
  
          let car = Car()
  
          &lt;other code goes here&gt;
     }
  
     &lt;other code goes here&gt;
  }
  
  </code></pre>
  
  <p>As you can see I've imported the module itself using the keyword <code>@testable</code>. But when I run <code>swift test</code> command I'm getting this weird error:</p>
  
  <pre><code>Compile Swift Module 'SwiftExample' (2 sources)
  Compile Swift Module 'SwiftExampleTests' (2 sources)
  Linking ./.build/x86_64-apple-macosx10.10/debug/SwiftExample
  /Users/artem/Playgrounds/SwiftExample/Tests/SwiftExampleTests/SwiftExampleTests.swift:9:13: warning: initialization of immutable value 'car' was never used; consider replacing with assignment to '_' or removing it
          let car = Car()
          ~~~~^~~
          _
  Linking ./.build/x86_64-apple-macosx10.10/debug/SwiftExamplePackageTests.xctest/Contents/MacOS/SwiftExamplePackageTests
  Undefined symbols for architecture x86_64:
    "_$S12SwiftExample3CarCACycfC", referenced from:
        _$S17SwiftExampleTestsAAC04testB0yyKF in SwiftExampleTests.swift.o
    "_$S12SwiftExample3CarCMa", referenced from:
        _$S17SwiftExampleTestsAAC04testB0yyKF in SwiftExampleTests.swift.o
  ld: symbol(s) not found for architecture x86_64
  &lt;unknown&gt;:0: error: link command failed with exit code 1 (use -v to see invocation)
  error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/artem/Playgrounds/SwiftExample/.build/debug.yaml test output:
  </code></pre>
  
  <p>I'm certainly doing something wrong here but I can't find any information on the matter in the official docs. Does somebody know what's happening here and how to fix that?</p>