Tirna
Tirna

Reputation: 533

Debugging window doesn't show print statements

I've been able to see print statements in the debugging window for my app. When I create a 'mock up' program (small trial app) to learn about Swift testing, none of the print statements in my LifecycleTests.swift file under the FirstTests folder display in the debugging window.

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.
        }
    }
}

Upvotes: 7

Views: 1982

Answers (1)

matt
matt

Reputation: 534893

You can only get console output from one target at a time. Your console output is set to come by default from your app target, not your test target.

If you just run a test containing a print statement, you won't see any debugger output:

enter image description here

That test has a print statement, but we ran it and nothing appeared in the console.

Okay, but now let's trick the console into seeing the input from the test target. To do so, put a breakpoint in the test:

enter image description here

We step over the print statement and it prints to the console, along with a lot of other useful info about the test:

enter image description here

The interesting thing is that once you've used this trick, you can take the breakpoint away. The test target continues to be the console source until you run the app itself again.

The trick is a weird one but it appears to be the only way. Apple actually acknowledges it in an allusive way in their docs where they say:

if you have been actively engaged in debugging, any output from the debugging session also appears [in the console]

Evidently "actively engaged in debugging" means you've put a breakpoint in the test.

Upvotes: 15

Related Questions