簡體   English   中英

為什么 Xcode 找不到我的單元測試應用程序?

[英]Why can't Xcode find my App for Unit Tests?

我對 iOS 相當缺乏經驗。 我想為我的 Cocoapods 項目編寫單元測試。 在此處輸入圖像描述

我要測試的 ViewController 位於 Pod 中。 當我想編寫單元測試時,我已經收到了一條帶有import MyApplication的錯誤消息

它說

No such module 'MyApplication'

請注意,我僅使用“MyApplication”代替我在圖片中用紅色划掉的原始名稱。

問題是什么?

編輯:

我的測試代碼 class:

import UIKit
import XCTest
import Pods_MyApplication_Example

@testable import Pods_MyApplication

class Tests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // This is an example of a functional test case.
        XCTAssert(true, "Pass")
    }

    func testPerformanceExample() {
        // This is an example of a performance test case.
        self.measure() {
            // Put the code you want to measure the time of here.
        }
    }

}

要創建測試目標,請在測試導航器上單擊“+”,然后選擇“新建單元測試目標”

在此處輸入圖像描述

如果有任何信息丟失,我很抱歉。 有的話我會加的!

EDIT2:更改的圖片可能會提供更多信息

您必須使用@testable import來導入應用程序的模塊以進行單元測試。 您必須提供應用程序目標的名稱 Pods_MyApplication_Example,而不是項目的名稱。 在您的示例中,它將如下所示:

@testable import Pods_MyApplication_Example

並刪除導入應用程序目標的導入語句。

感謝您的回復。 我必須將要測試的插件/應用程序添加到我的 Podfile 中的測試目標。 所以 Podfile 看起來像這樣:

use_frameworks!
target "MyApplication_tests" do
  pod 'MyApplication', :path => '../'
end

運行pod update后,我可以導入並測試我的應用程序。

當您沒有出現正確的測試主機時,也會發生此錯誤。

如果您看到這一點,並且您選擇了 iOS 模擬器或 iPadOS 模擬器。 確保您的測試目標支持該平台。

  • Select 左側瀏覽器中的 Xcode 項目。

  • 在項目的常規選項卡中單擊您的測試目標。

  • 在“常規”select 下,您的相應主機應用程序在
    “測試”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM