Welcome back.
Let's talk about a few more neat things that you can do with unit test.
And remember, you can always go to the documentation on the Python website and learn more about some
of the things that you can do.
But notice here how at the end we added the if name may.
Some of you may be wondering why this is useful, because so far we've just ran it from the command
line as a single file and that has been working.
But usually you have more than one file and you have each module.
Test it with different functions, right?
So ideally you want to run all these tests in unison and the way you do this is to write Python.
And then dash em and then give it the unit test that you want to run.
So now if I do this, you see that everything is working.
Let's say I add a new file here.
Let's say touch test two dot py, which is a short command on Macs to just create a new file.
So now my desktop.
Let's just copy this test that we originally have.
Go to my desktop and open up this in sublime text.
So now if I copy and paste this the exact same thing.
And I say this.
Go back to my code and run.
Unit test.
You see that?
I have eight tests now that are being run and because I've added this if name mean, I'm able to run
all these tests with just one simple command.
Now another neat thing is that we can do something called Dash V, which stands for Verbose, and that
is when we do this, we actually get more information about the test that we ran.
So it gives you a little bit more detail.
You see which tests we've run and which ones are okay, which ones have failed.
As a matter of fact, I can even add comments and doc strings to my tests.
So let's say test do stuff.
I can say single code, single code three times and simply say hi.
If I run this again with the Dash V command.
Oh, and this has to be on a single line, I believe.
Let's save that again and run this.
Look at that.
I also get some comments, so maybe I wanted to add a few comments on my tests.
I can add those as well.
What other neat thing can I do?
Another default method that we get with unit test is something called set up.
And set up.
Allows us to run a piece of code.
That sets up before each call of the test.
So, for example, whatever I write in here, let's say I wanted to set up my test and just print about
to test a function.
Now because I have this if I run my code now.
You see that before each method?
I run the setup method first.
So you see that I say about to run a function, about to run a function about to run a function and
about to run a function.
And then we have our test to file that we haven't done any setup for.
So this is really, really useful.
If you need to set up something before each function.
Let's say you have some default variables maybe that you need to set up.
Well, in that case, this is a very useful method.
Another one just like setup.
Usually you add.
At the bottom of the file.
And in here, this is called a tear down.
And as the name suggests, we run it at the end of each method that we call.
So in here, let's say cleaning up.
And usually you do this to clean up some variables, maybe reset some variables.
If I run this.
You see that I have now about to test the function.
And then we're cleaning up for each one of these methods.
So these two are very, very useful when setting up and tearing down tests.
Now tear down.
You won't use as often.
Usually you use it if you're testing something more complicated, like a database.
But we won't worry about that too much now.
All right.
There you have it for tests, as you can see at the end of the day.
Tests are simply logical programs that we write, right?
It's just a class.
And then we set up all these methods to try and test our code as much as possible.
In our case, we just had one simple function, but by using tests, we're able to notice small things
that can go wrong with the function and thus improve our code so that next time somebody comes, another
developer comes and tries to modify this function.
Or maybe tries to call this function with something that is not right.
Well, our tests are going to fail and it's going to let the developer know, hey, you're doing something
wrong.
Somebody who wrote this function before you.
Somebody that wrote these tests before you.
Told you to use this function in a specific way.
Very, very useful.
In the next video, we're going to do a fun little exercise to make sure we understand testing.
I'll see you in the next one.
Bye bye.