In this video, I will show you how to
write and run tests on your code. In a
real project we would have spent a
considerable amount of time writing
tests for our code, to make sure that it
functioned like we expected it to. Let me
show you how to do that now. Let's write
a test to make sure that our as time
method for our time output trait
functions like we expect it to. I'm going
to start by creating an inline sub
module named tests, it could be named
anything as long as it has this
attribute, config test, that means that
this module only compiled during tests.
So when we're running our release code,
this isn't even in the binary. Next since
this is a sub module, we'll need to use
from super our time output trait,
otherwise we won't be able to see it.
Next we need a function to do the actual
testing, let's just call it as time
format, the name doesn't matter, again we
just need to have an attribute. As long
as we have the test attribute on our
function it will be run as a test. Let's
test a variety of inputs at once, so I'll
create a vector and inside the vector
I'll put tuple pairs, and for each tuple
the first member will be a u 64 to use
this input, and then a string to test the
output. Then we just copy and paste this
several times, and we have our data to
work with. Of course, we need to set these
all to different values, so let's set
this to one minute which is 60 seconds,
and then let's choose a hundred and
fifty four and 3603,
that'll test multiple minutes, and over
an hour, 154 is two minutes and 34
seconds, and 3603 is a one hour in three
seconds. Now we can go write our loop. So
for input and output in pairs, we assert
equal and now you can call our function,
as time, and let's also do as str so it
will compare with the Equality operator,
and put in our output and that's it that
will loop through each of these
to make sure that we've got the rest of
this looking okay. That's all good,
let's go run our tests. Running them is
easy, it's just a matter of Cargo tests
that will build everything in test mode
and then run it, and there we go, so we
can see that we've run our one test as
time format which got an okay, we also
ran doc tests which are tests embedded
and documentation of which we have zero,
because that is next. In the next video, I
will show you how to document your code.