调试窗口不显示打印语句

Tir*_*rna 4 debugging xcode unit-testing swift

我已经能够在我的应用程序的调试窗口中看到打印语句。当我创建一个“模拟”程序(小型试用应用程序)来了解 Swift 测试时,FirstTests 文件夹下的 LifecycleTests.swift 文件中的任何打印语句都不会显示在调试窗口中。

import XCTest

class LifecycleTests: XCTestCase {

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

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

    override func setup() {
        print("In setup.")
        // NSLog("In setup")
    }

    override func tearDown() {
        print("In tearDown.")
        // NSLog("In tearDown.")
    }

    func testExample() {
        print("Starting test.")
        // NSLog("Starting test.")

        addTearDownBlock {
            print("In first tearDown block.")
            // NSLog("In first tearDown block.")
        }

        // print("In middle of test.")
        NSLog("In middle of test.")

        addTearDownBlock {
            print("In second tearDown block.")
            // NSLog("In second teardown block.")
        }

        print("Finishing test.")
        // NSLog("Finishing test.")
    }

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

mat*_*att 11

您一次只能从一个目标获取控制台输出。您的控制台输出默认设置为来自您的应用程序目标,而不是您的测试目标。

如果您只是运行包含语句的测试print,您将不会看到任何调试器输出:

在此输入图像描述

该测试有一个print声明,但我们运行它并且控制台中没有出现任何内容。

好的,但现在让我们欺骗控制台来查看来自测试目标的输入。为此,请在测试中放置一个断点:

在此输入图像描述

我们跳过该print语句并将其打印到控制台,以及有关测试的许多其他有用信息:

在此输入图像描述

有趣的是,一旦你使用了这个技巧,你就可以取消断点。测试目标仍然是控制台源,直到您再次运行应用程序本身。

这个技巧很奇怪,但似乎是唯一的方法。苹果实际上在他们的文档中以暗示的方式承认了这一点,他们说:

如果您一直在积极进行调试,调试会话的任何输出也会显示在[控制台中]

显然,“积极参与调试”意味着您在测试中设置了断点。