Welcome back to our series on Xcode 9. Today we'll talk about automated tabs. To cover that in a live way, we'll also cover debugging and testing.

Debugging

So here's some code that I've written, and I'd like to step through it one line at a time.

Breakpoint

So I hit the play button, and Xcode will build the app, start running it in the iPhone Simulator.

And when it hits the break point, it will automatically open the stack navigator, and show me where the current place is in the code.

stack navigator

Variables View

It will show me the values that I've got, and as I step over each one...

variables view

Ok, now width is filled in.

I'll step over another one, now height is filled in.

LLDB / Console

And if I want to get a more detailed description of something, I can go over to the console, and type "po", which used to stand for "print object" in Obj-C. When they introduced Swift, they said, "well, people are just used to typing 'po' to get what they want, so... we'll just use that."

So I'll type some Swift code after that. I even get code completion in the console, which is very nice! I hit enter, and now Swift is going to compile that, and display results.

print object console

So in this example, it's taken the key object, and displayed a debug description of it in the console for me. You can learn more about this by learning more about lldb, which stands for "low-level debugger".

Also I have controls for stepping into and out of, and I'm going to skip detailed descriptions of those.

debugging controls

So that's debugging.

Testing

I also want to show you testing. I've got some code in a test bundle here, and I want to be able to step through that.

Tests get a nice little button to their left. I can just click that to build and run just that test.

running a test

Xcode will build not just app code, but also the test bundle, and run the tests.

Now Xcode has stopped at the breakpoint in my test, and I can step through my tests as well.

breakpoint in test

I'll set a breakpoint inside this closure, and this lets me, in the debugger, access the internal properties after my code makes a change to its value.

internal properties via debugger

When tests succeed, I'll get this popup,

Test Succeeded

and also a green checkmark next to the succeeding tests.

Succeeding Tests

If I were to alter the test so it fails, I would get a "Tests Failed" popup, and a red "X" next to any failing tests.

Test Failed

Failing Tests

Behaviors

It was nice that Xcode brought up the issues navigator when there was a warning. And during debugging, it opened the debug tab, and it displayed the debug stack. But I would really have liked to have seen the tests as they executed.

So let's configure that.

You can get to it in two ways from the Xcode menu. You can either click "Preferences..." and then behaviors, or just behaviors directly.

Xcode 9 Preferences

and I'm going to select the case of starting the test.

Starting Tests

I'll tell it to show a tab named "Tests", and this is actually going to create a tab with this name if one does not already exist.

Then I'm going to tell it to show the test navigator. I have the option of all the navigators that show up in the left-hand bar. I'll just pick the one I want open.

Configuration of tests

So I'll go back to my source code for my test. I'll fix the value so it should pass the next time, run it again.

Xcode builds the tests again, and it creates a new tab for me named "Tests", where it shows the list of all tests and the status for each one.

Tests Tab

Fantastic!

Test logs

If I have a lot of tests, and I want to dig deep into, maybe why a test failed, I go to the "Report Navigator" and click the most recent test. I get a bird's eye view of the status of each test here, but I click, "Logs", and now I can open into each test.

Test Logs

Xcode automated testing groups tests by bundle, class and method.

If I click this top-secret button which only shows up when the test is selected, then I get a complete log of everything printed to the console while the test was running..

So maybe I could add a super helpful debug message to my test to help me understand what might have gone wrong if the test fails.

Debug Messages

I'll run the tests again, to demonstrate. Build, run, test, succeed, log, Logs again, expand the test again, click the secret button, and there's my super helpful debug message.

Debug messages in tests log

Conclusion

So that's a quick tour of debugging, testing and automated creation of tabs when you need them. Next time, we'll learn about Xcode's built-in code repository UI.