I do not assume I’ve ever heard of a testing library that does not have some mechanism to check assertions. An assertion within the context of testing is actually an assumption that you’ve about your code that you just need to guarantee is appropriate.
For instance, if I had been to jot down a operate that is supposed so as to add one to any given quantity, then I’d need to assert that if I put 10 into that operate I get 11 out of it. A testing library that might not have the ability to do that isn’t value a lot. And so it needs to be no shock in any respect that Swift testing has a method for us to carry out assertions.
Swift testing makes use of the #anticipate
macro for that.
On this put up, we’re going to check out the #anticipate
macro. We’ll get began through the use of it for a easy Boolean assertion after which work our method as much as extra complicated assertions that contain errors.
Testing easy boolean situations with #anticipate
The commonest method that you just’re in all probability going to be utilizing #anticipate
is to be sure that sure situations are evaluated to betrue
. For instance, I’d need to take a look at that the operate under really returns 5 at any time when I name it.
func returnFive() -> Int {
return 0
}
In fact this code is just a little bit foolish, it does not actually try this a lot, however you could possibly think about {that a} extra sophisticated piece of code would must be examined extra totally.
Since I have not really applied my returnFive
operate but, it simply returns 0. What I can do now could be write a take a look at as proven under.
@Take a look at func returnFiveWorks() async throws {
let functionOutput = Incrementer().returnFive()
#anticipate(5 == functionOutput)
}
This take a look at goes to check that after I name my operate, we get quantity 5 again. Discover the road the place it says #anticipate(5 == functionOutput)
.
That’s an assertion.
I’m attempting to claim that 5 equals the output of my operate through the use of the #anticipate
macro.
When our operate returns 5, my expression (5 == functionOutput
) evaluated to true
and the take a look at will go. When the expression is false
, the take a look at will fail with an error that appears a bit like this:
Expectation failed: 5 == (functionOutput → 0)
This error will present up as an error on the road of code the place the expectation failed. That signifies that we will simply see what went mistaken.
We are able to present extra context to our take a look at failures by including a remark. For instance:
@Take a look at func returnFiveWorks() async throws {
let functionOutput = Incrementer().returnFive()
#anticipate(5 == functionOutput, "returnFive() ought to all the time return 5")
}
If we replace our exams to look just a little bit extra like this, if the take a look at fails we’ll see an output that is a little more elaborate (as you’ll be able to see under).
Expectation failed: 5 == (functionOutput → 0)
returnFive() ought to all the time return 5
I all the time like to jot down a remark in my expectations as a result of this can present just a little bit extra context about what I anticipated to occur, making debugging my code simpler in the long term.
Typically talking, you are both going to be passing one or two arguments to the anticipate macro:
- The primary argument is all the time going to be a Boolean worth
- A remark that will probably be proven upon take a look at failure
So within the take a look at you noticed earlier, I had my comparability between 5 and the operate output within my expectation macro as follows:
5 == functionOutput
If I had been to vary my code to appear to be this the place I put the comparability exterior of the macro, the output of my failing take a look at goes to look just a little bit completely different. Here is what it should appear to be:
@Take a look at func returnFiveWorks() async throws {
let functionOutput = Incrementer().returnFive()
let didReturnFive = 5 == functionOutput
#anticipate(didReturnFive, "returnFive() ought to all the time return 5")
}
// produces the next failure message:
// Expectation failed: didReturnFive
// returnFive() ought to all the time return 5
Discover how I am not getting any suggestions proper now about what might need gone mistaken. I merely get a message that claims “Expectation failed: didReturnFive” and no context as to what precisely might need gone mistaken.
I all the time advocate attempting to place your expressions contained in the anticipate macro as a result of that’s merely going to make your take a look at output much more helpful as a result of it should examine variables that you just inserted into your anticipate macro and it’ll say “you anticipated 5 however you’ve got bought 0”.
On this case I solely know that I didn’t get 5, which goes to be quite a bit tougher to debug.
We are able to even have a number of variables that we’re utilizing within anticipate and have the testing framework inform us about these as properly.
So think about I’ve a operate the place I enter a quantity and the quantity that I need to increment the quantity by. And I anticipate the operate to carry out the maths increment the enter by the quantity given. I might write a take a look at that appears like this.
@Take a look at func incrementWorks() async throws {
let enter = 1
let incrementBy = 2
let functionOutput = Incrementer().increment(enter: enter, by: incrementBy)
#anticipate(functionOutput == enter + incrementBy, "increment(enter:by:) ought to add the 2 numbers collectively")
}
This take a look at defines an enter variable and the quantity that I need to increment the primary variable by.
It passes them each to an increment operate after which does an assertion that checks whether or not the operate output equals the enter plus the increment quantity. If this take a look at fails, I get an output that appears as follows:
Expectation failed: (functionOutput → 4) == (enter + incrementBy → 3)
increment(enter:by:) ought to add the 2 numbers collectively
Discover how I fairly conveniently see that my operate returned 4, and that isn’t equal to enter + increment
(which is 3). It is actually like this stage of element in my failure messages.
It’s particularly helpful whenever you pair this with the take a look at arguments that I lined in my put up on parameterized testing. You’ll be able to simply see a transparent report on what your inputs had been, what the output was, and what could have gone mistaken for every completely different enter worth.
Along with boolean situations like we’ve seen to this point, you may need to write exams that verify whether or not or not your operate threw an error. So let’s check out testing for errors utilizing anticipate subsequent.
Testing for errors with #anticipate
Generally, the aim of a unit take a look at is not essentially to verify that the operate produces the anticipated output, however that the operate produces the anticipated error or that the operate merely does not throw an error. We are able to use the anticipate
macro to claim this.
For instance, I might need a operate that throws an error if my enter is both smaller than zero or bigger than 50. Here is what that take a look at might appear to be with the anticipate
macro:
@Take a look at func errorIsThrownForIncorrectInput() async throws {
let enter = -1
#anticipate(throws: ValidationError.valueTooSmall, "Values lower than 0 ought to throw an error") {
attempt checkInput(enter)
}
}
The syntax for the anticipate macro whenever you’re utilizing it for errors is barely completely different than you may anticipate primarily based on what the Boolean model seemed like. This macro is available in numerous flavors, and I want the one you simply noticed for my normal function error exams.
The primary argument that we go is the error that we anticipate to be thrown. The second argument that we go is the remark that we need to print at any time when one thing goes mistaken. The third argument is a closure. On this closure we run the code that we need to verify thrown errors for.
So for instance on this case I am calling attempt checkInput
which signifies that I anticipate that code to throw the error that I specified as the primary argument in my #anticipate
.
If every part works as anticipated and checkInput
throws an error, my take a look at will go so long as that error matches ValidationError.valueTooSmall
.
Now as an instance that I unintentionally throw a unique error for this operate the output will look just a little bit like this
Expectation failed: anticipated error "valueTooSmall" of kind ValidationError, however "valueTooLarge" of kind ValidationError was thrown as an alternative
Values lower than 0 ought to throw an error
Discover how the message explains precisely which error we obtained (valueTooLarge
) and the error that we anticipated (valueTooSmall
). It is fairly handy that the #anticipate
macro will really inform us what we obtained and what we anticipated, making it straightforward to determine what might have gone mistaken.
Including just a little remark similar to we did with the Boolean model makes it simpler to cause about what we anticipated to occur or what could possibly be occurring.
If the take a look at doesn’t throw an error in any respect, the output would look as proven under
ExpectMacro.swift:42:3: Expectation failed: an error was anticipated however none was thrown
Values lower than 0 ought to throw an error
This error fairly clearly tells us that no error was thrown whereas we did anticipate an error to be thrown.
There is also conditions the place you do not actually care in regards to the precise error being thrown, however simply that an error of a selected kind was thrown. For instance, I won’t care that my “worth too small” or “worth too giant” error was thrown, however I do care that the kind of error that bought thrown was a validation error. I can write my take a look at like this to verify for that.
@Take a look at func errorIsThrownForIncorrectInput() async throws {
let enter = -1
#anticipate(throws: ValidationError.self, "Values lower than 0 ought to throw an error") {
attempt checkInput(enter)
}
}
As an alternative of specifying the precise case on validation error that I anticipate to be thrown, I merely go ValidationError.self
. It will permit my take a look at to go when any validation error is thrown. If for no matter cause I throw a unique type of error, the take a look at would fail.
There is a third model of anticipate in relation to errors that we might use. This one would first permit us to specify a remark like we will in any anticipate. We are able to then go a closure that we need to execute (e.g. calling attempt checkInput
) and a second closure that receives no matter error we obtained. We are able to carry out some checks on that after which we will return whether or not or not that was what we anticipated.
For instance, when you have a bit extra sophisticated setup the place you are throwing an error with an related worth you may need to examine the related worth as properly. Here is what that would appear to be.
@Take a look at func errorIsThrownForIncorrectInput() async throws {
let enter = -1
#anticipate {
attempt checkInput(enter)
} throws: { error in
guard let validationError = error as? ValidationError else {
return false
}
swap validationError {
case .valueTooSmall(let margin) the place margin == 1:
return true
default:
return false
}
}
}
On this case, our validation logic for the error is fairly primary, however we might increase this in the actual world. That is actually helpful when you may have a sophisticated error or sophisticated logic to find out whether or not or not the error was precisely what you anticipated.
Personally, I discover that normally I’ve fairly simple error checking, so I’m usually utilizing the very first model of anticipate that you just noticed on this part. However I’ve positively dropped all the way down to this one after I wished to examine extra sophisticated situations to find out whether or not or not I bought what I anticipated from my error.
What you want is, in fact, going to rely by yourself particular scenario, however know that there are three variations of anticipate that you need to use when checking for errors, and that all of them have form of their very own downsides that you just may need to keep in mind.
In Abstract
Normally, I consider testing libraries by how highly effective or expressive their assertion APIs are. Swift Testing has performed a extremely good job of offering us with a reasonably primary however highly effective sufficient API within the #anticipate
macro. There’s additionally the #require
macro that we’ll discuss extra in a separate put up, however the #anticipate
macro by itself is already a good way to begin writing unit exams. It gives lots of context about what you are doing as a result of it is a macro and it’ll increase into much more data behind the scenes. The API that we write is fairly clear, fairly concise, and it is highly effective to your testing wants.
Be sure to take a look at this class of Swift testing on my web site as a result of I had lots of completely different posts with Swift testing, and I plan to increase this class over time. If there’s something you need me to speak about by way of Swift testing, ensure you discover me on social media, I’d love to listen to from you.