Episode #330

Using XCTest in Playgrounds

Series: Testing iOS Applications

4 minutes
Published on March 9, 2018

This video is only available to subscribers. Get access to this video and 586 others.

Running your tests in Xcode Playgrounds can be a great way to get rapid feedback on your changes. It is a useful way of practicing testing and experimenting with APIs that you are not familiar with. In this episode we will see how to run the test suite and how to enhance the feedback we get by observing the test results as they are run.

This episode uses Swift 4, Xcode 9.2.

Sometimes it can be helpful

to whip up a quick test

within an Xcode playground

in order to validate

that we're using an API correctly

or just to get some code on the screen

so that we can actually

start to flesh out an idea.

And so we can actually

use Xcode playgrounds

for this to come up with

really quick examples

without creating entire projects.

So I'm going to cover that right here.

So the first thing we want

to do is import XCTest

and we're going to create our test case.

I'm going to call this my test case

and that will inherit from XCTest Case.

And then we will have two tests.

We'll say test should fail

and here we'll say XCTAssert fail or false

and I'll put true and we'll

say forced failure there.

And then we'll say test should pass.

XCTAssert true, true.

This one passes.

So we've got a test case that

should have one failing test

and one passing test.

So in order for us to run these,

we can, on our test case type,

we can create a default test suite

and then call run on that.

So we can see that the

tests are running here

in the playground output,

but it's a little bit hard to see

when things failed and passed

because there's other text around it.

So, what we can do is

register a test observer

and give ourselves a better

output if a test fails.

And so we're going to do that

by creating our own test observer

that needs to inherit from NS object

and conform to the XCTest

observation protocol.

This is going to give us access to methods

like the test case did finish,

the test case did fail, or

the entire suite failed.

So in this case, test case

did fail with description

and we're going to print

out some emoji here

that indicates the failure

and we're also going to paste in

the description of the failure.

That's going to be the

description that comes from here,

from this failure message.

And then also the line

number that it happened.

Because we're in a playground,

it's going to be in the same file

so I can just say line number like that

and we can say line, line number

so it'll be like there's

a test failure at line 27.

And in order for us to

use this test observer,

we can create an instance of it.

And then we can use the XCTest

observation center class,

get the shared instance of that,

and then add a test observer

to that observation center.

And then now when our test fails,

you can see that we

actually get this emoji.

New line here, so we

can actually make this

a little bit more obvious

when a test fails.

And then in order for us

to detect if a test passed,

there's the test case did finish.

And the case here is

that if it didn't fail

by the time we get to the

end, then it succeeded.

And we can check that to see

if the test case dot test run

and that's optional because

it may not have completed,

this will tell us the execution count,

the failure count, whether it succeeded,

dates and times, things like that,

so you can measure them.

There's lots of good information here,

but we just want to see if it succeeded,

then we want to print out that

this test case did succeed.

We're going to print out the check mark

and then the test case itself

is probably sufficient for that.

So, in this case, you can

see that we got one test

that passed, XCTAssert false failed.

That's our forced failure.

And then my test case

test should pass, passed.

So this is a good way of using playgrounds

to just really quickly,

as we type our code,

it's going to run our test

every time we type our code,

it's going to run our test.

So this is going to be a really handy way

to practice testing and to test things

that you may not want to

create a whole project for.