0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ローカルSPMモジュールのテストカバレッジを取得する

Last updated at Posted at 2025-03-02

はじめに

ローカルのSPMモジュールに依存しているプロジェクトで、テストを実施した際に、SPMモジュールのテストカバレッジが一部取れていなかった、かつテストコードのカバレッジまで含まれていて、イマイチだったので、その時の対処法を備忘の意味も込めて記事にしました。

前提

  • Xcode16.2

  • Swift6

  • プロジェクト構成
    image.png

  • SPM マルチモジュール構成

// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "PackageModule",
    platforms: [.iOS(.v18)],
    products: [
        .library(
            name: "MainFramework",
            targets: ["MainFramework"]
        ),
    ],
    targets: [
        .target(
            name: "MainFramework",
            dependencies: [
                "View",
                "Model"
            ]
        ),
        .target(
            name: "View",
            dependencies: [
                "Core"
            ]
        ),
        .target(
            name: "Model",
            dependencies: [
                "Core"
            ]
        ),
        .target(
            name: "Core"
        ),
        .testTarget(
            name: "ViewTest",
            dependencies: ["View"]
        ),
        .testTarget(
            name: "ModelTest",
            dependencies: ["Model"]
        ),
    ]
)

遭遇した問題

テスト実行した時に、カバレッジを確認するとテストコードが含まれており、SPM モジュールの一部のプロダクションコードがカバレッジ範囲から外れていました。

image.png

修正方法

カバレッジ対象のターゲットをプロダクションコードに絞る

edit schemeから紐づいているtestplanの設定を開いて、以下のようにcode coverageの設定をall targetsからsome targetsにしてテストターゲット以外のカバレッジを取りたいターゲットを指定するようにします。

image.png

Package.swiftを修正する

MainFrameworkのバンドルに含めるターゲットに、カバレッジを取りたいターゲットを全て含めることで、
カバレッジを取得するターゲットをMainFrameworkに指定した際、バンドルに含めたターゲットもカバレッジ取得の対象になるようです。

// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "PackageModule",
    platforms: [.iOS(.v18)],
    products: [
        .library(
            name: "MainFramework",
-           targets: ["MainFramework"]
+           targets: [
+               "MainFramework",
+               "View",
+               "Model",
+               "Core"
+           ]
        ),
    ],
    ・・・省略
)

結果

テストコードをのぞいた、SPMモジュールのプロダクションコードのカバレッジを取得できるようになりました

image.png

今回のサンプルコードは以下リポジトリにも挙げているので、手元で確認してみたい方はご参照ください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?