Welcome back.
Let's write our first tests now.
We're going to just have a simple function that we run in this program.
We'll create in our main file a let's say let's make this a little bit bigger.
We'll say that we have a function called do stuff.
Yes, very descriptive function.
Now in here, we'll just say that we accept a number.
And we'll just simply return num plus five.
A very, very simple function.
So let's say this was our code that builds our app.
So this is just one function.
But hypothetically, we have many functions that we want to test because these are actions that we're
going to perform in our code.
So the way unit test works.
Is like this.
We have our test file and after we import our unit test, we want to import well the file that we want
to test in our case it's main py.
So let's just import.
Main.
Which is right here.
Or we can just import the function as well if we want.
But in our case, hypothetically, we have many functions here, so we can just import main and use
all the functions available there.
Now the way unit test works is we create a class and then name it whatever you want.
And here we'll have all our tests.
So let's just call it Test Main.
And we inherit inside of this class what unit test gives us, which is a test.
So this is just a standard way to work with unit test.
So we're inheriting whatever unit test test cases and now we have that functionality available in our
test main class.
Now in here we can start testing and writing code to test.
So how can we test main?
Do stuff function.
Well, we simply say, hey, let's write a function in here or a method because this is part of a class
and say, Hey, I want to test, do stuff.
And in here it will receive self.
Now in here, I can test my code so I can say if the number in here is equal to ten, I'm just giving
it a random number.
Then I'm going to say result is going to equal main dot do stuff.
So remember, do stuff comes from Maine, which is the function that we want to test.
So I want to run the do stuff function and in here give the parameter num, which I've just given here
or let's just call this test param.
So this test param, we're going to run the code and we're going to get a result.
And this last line is the key part of unit test.
Unit test allows us to say self dot, assert equal and assert equal is something that we get when we
inherit from unit test and assert equal says hey assert.
Or make sure that these two parameters are equal.
In our case, I want to test that the result is equal to well, in our case, ten.
Plus five should equal 15.
All right.
Now, finally in here, we just run the unit test.
And the way we run the unit test, because this is just one test that I did.
I could have done multiple tests.
All we need to say is unit test dot main.
Which will run the entire test file within the test main.
Now, if I run this now, all I have to do is say Python three, my command line and say Test dot pi.
And I get.
OC ran one test in well super fast and 0 seconds.
That was nice and quick.
Now let's say I do something else where I assert equal that this equals ten.
If I run this.
Look that.
I get a fail?
What fail?
Well, the test to do function failed.
On line eight.
So that is right here we have an assertion error.
We got 15, but we expected ten.
How cool is that?
So I now have one failure, so I need to fix my code.
So something's not right here.
Now, in my case, this is because I've asserted.
The answer to B 15.
But what if we create another test in here?
And check.
Hey, what if we have test do stuff too.
And this time the test program is actually going to be a piece of string.
So what do we want the function to do?
Well, in our case, maybe it gives us a error, maybe a value error.
So I'll just say value.
Because this shouldn't work.
If I run this now, let's clear this.
If I run this now.
All right, I have two tests.
But I have one heir and I see here that I'm getting an heir saying that, hey, I'm getting a typer
can only concatenate string, not eight.
Now, in this case, what actually fails?
Is the test file because what we've just done actually fails are function.
But right away.
I see that.
All right.
Well, this is something that we need to fix so I can go back to Maine and say, hey, I want to make
sure that we catch any errors.
Like type errors or value errors.
So the first thing I can do is just say aren't.
Number.
And if I save that, to make sure that we convert something to an integer and I run this.
All right, now I get a value error in value literal so it can't be run on this piece of string.
Okay, so another thing we can do to improve our code is to wrap it in a try except block write and
then we can catch.
So let's say except.
And in here accept a value error.
And let's say as error.
And in here, we can do whatever we want.
So we can return if we want the error or we can even raise the air.
In our case, let's just return the air.
And I'll show you why in a second.
Now, if I ran this.
You see that my test fails, but now it's actually an assertion error.
The assertion error is coming from our test file over here, and I'm asserting that, hey, I want to
make sure that the result and the value error are equal.
Now you see over here that there's a bit of a mismatch.
We have a value error over here doesn't equal the class value error.
Now this is a bit of a trick where because we're returning an error.
It's actually an instance of the value error class.
So we need to do something like this.
We need to say, hey, is instance.
And say, what is it an instance of?
Well, is result an instance of value error like this.
But the problem here.
Is that let's make this a little bit bigger.
The problem here is that assert equals is going to expect two parameters, two things that must be equal.
But here I just want to make sure that this returns true, that the result is an instance of value error.
So how can I do that?
Well, besides assert equal, we have a few other methods that unit test gives us.
If I go to the unit test documentation here on the first line, it says the list of assert methods.
So here I see that these are all the methods for asserting and testing our code.
The one that we probably want is this one assert true.
To check if something is true.
So if I do assert.
That this is true.
And I ran my test.
Let's clear this and run my test again.
There you go.
I ran two tests and everything seems to be okay.
And again, to go over this.
Why is this?
Well, I'm running my test file.
And then I'm going to run the test, do stuff which is going to be okay.
Then I do the second part where the test parameter is something that we don't really want, which is
just a piece of string.
The result is going to give us a value error because we've caught this error and we're now returning
the error.
And then finally, we run this piece of code.
Hey, is this true?
Is the result going to be a value error?
So you see what we're doing here.
What we're trying to do with our tests is to improve our function by simply trying to break it.
So we want to break our function and see, Hey, can I trick this function into giving me an air?
And then once you do that and a test fails, then you go back to a function and try to fix it.
Kind of like we did here, which is really, really cool.
This is a common practice with testing where you try and break things until, well, the function becomes
perfect.
Now I want you to take your time here and see if you can break this function and maybe add a couple
more tests in the next video.
I'm going to add a few more to make this function even better.
I'll see you in the next one.