7

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.
        }
    }
}
2
  • @matt After following your recommendations and adding a FirstTests scheme, the only errors I get when pressing CMD+U are: use of Unresolved identifier 'addTearDownBlock' for each addTearDownBlock statement in the testExample method (see code listing above). I'm following an example, I hope, to the letter. Commented Dec 17, 2019 at 1:14
  • Well you certainly won't get any print statements if you can't even get your test to compile. Print statements happen when you run. But you are not able to run because you can't even compile. Commented Dec 17, 2019 at 5:43

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.