 Okay, you're up to speed. You know what they are and you know why they're important, but just how do you make them? Well, in this video, I'm going to share with you the easiest way to get up and running with making reference unit tests in Python. We'll make our very first tests and at the end, I'm going to challenge you to build upon them in this follow along video. So let's dive into the screen and get testing in just a sec. Hello world, I'm The Serving Scratcher, teacher server programmer and on this channel, I help curious learners just like you along on their learning journeys. My promise to you was to show you how to use unit tests in Replet and we're going to be using Python. So let's jump across to the browser right now. Okay, I'm over here at replet.com, a wonderful website where you can test your code in a browser. If you're not familiar with Replet, I've got a card coming away in the top corner that will get you up to speed, but if you're serious about your development and you want a quick way to test, by all means, go check it out. I'll also link to it down in the description. So I've just got a blank Python project here and if you don't have one, you can just hit create and make sure that you click Python and then put the name and create Repple. And that will take you to this screen here. I'm going to be working in mean.py and this demo is about time conversion and what I want to convert is our hours to minutes. So let's go ahead and define a function and we're going to call it convert hours to minutes. We're going to pass it a parameter of hours. Quite simply, we just want to return our hours multiplied by 60. And I suspect that this is the formula to convert hours to minutes. But this parameter here hours, it could be anything. I'm not explicitly typing it here. And the way I'm going to get around this is to create some unit tests. And the way that we do that in Replet is over here in the left sidebar. Down the bottom where you see this check mark with this pop out that says unit tests, I'm going to click that. Now I'm going to hit the set up button and you can see here it's going to import unit test and from main we're going to import every function. If you had some other files that you would like to import, you could just do that. You could copy this line and paste it down here and then just type in the name of the file that you want to reference. But I'm not going to do that because I'm just going to do everything inside of main. Now what I'd like you to do as well is hit this button. It's the docs. When you click this, it's going to take you to a Replet documentation page and it's going to go over the reasons why you would bother unit testing and a bit of a tutorial of how you can get up and running. Now down the bottom there is some tips for writing great tests and tips for writing testable functions. So just read through those dot points here. And all this isn't new here. We're breaking our program down into discrete functions into smaller parts, okay? We're keeping them concise and descriptive and we're designing predictable inputs and outputs which is what our convert hours to minutes function does. Another important link here is the Python unit test assert methods. If you're using Java or Node you can click on those but I'm here in Python so I'm going to click on this. And what this does is it takes you down to all the different methods that we can use and this isn't immediately clear right now. So let's jump back across to Replet to build up a test. I'm back over here in Replet and recall that we just click this docs button. I want to go on a little rabbit hole tangent and we're fine with where we're at now. We're just going to import the functions from the main.py. So let's save that and what we want to do is down here hit add test. We'll just pause adding a name right now and I think the best thing to do is to show an example. So here Replet has just pre-filled some code for us self.assert equals and there's two parameters. We're passing in a function here add which I'm assuming adds one and two and we're assuming that the output of this is equal to the integer three. Now if the output of this function does not equal three then this will fail. This test will fail. So what we can do instead of passing in this add function we can pass in our convert hours to minutes. So what I'm going to do is just get our hours here and I'm going to set it to one and then I'm going to create another variable and I'm going to assign to that variable the result of convert hours to minutes. We're going to pass in our hours which we have just assigned to be one and now I want to check if the result of our minutes is equal to 60 of course because there's 60 minutes in one hour. Then we can go ahead and name our test. So we're saying that one hour equals 60 minutes and you can see that has become the name of our test. Test that one hour is equal to 60 minutes and then we've got this opportunity to put in a failure message and I've just put in one hour should equal 60 minutes. Check your function's logic. So let's go ahead and save that. I'll just open up the pane here and what we can do is click run tests and we've just got some green feedback here to say that this test has passed. All tests are passed one out of one. Let's go back into our test now and let's change the expected result to say 61. Now we know that our function is going to output 60 and we're asserting that that output of our function which is 60 is equal to 61. So let's just save that. Let's run our tests again and you can see that our test has now failed. I'll just go in quickly and revert our test back to the way it should be. So this is good but now I want to write a test that's going to check the logic of ours to make sure that the type of hours is going to be equal to an int or a float because we don't want it equal to any other value. So I'm going to go in and create a new test and I do that by up here clicking the addition symbol and let's go ahead and create our test. In fact what I might do is just copy the little stub here that I've got and then I'll go in and just paste that stub in straight away. So what I'm going to do now is test that our hours is going to be an int or a float here. We're going to assign our hours to be this string one and we're going to pass in hours into our function and we're going to assert that our minutes is equal to some kind of error string. I might say something like oops hours must be an int or a float. Hours is a and here I can see that ours is a string so I'm going to just specify that by doing a type of call there. So I'm going to copy this output. I need to give this test a name so what I'm testing here is that the hours it's a string now it's going to return an error string. The failure message will say something like hours must be either an int or a float. Let's save that. Now I'm going to run my tests and you can see my one hour equal 60 minutes test is passed but now I need to make sure I pass this test. The way I'm going to do that is to check the type of the hours parameter. So I'll say something like if is not instance of and what I want to do here is pass in that hours parameter and this particular function asks for a tuple and we can pass in a tuple of types and I'm just going to pass in a float or an int. So if our hours parameter is not an int or a float then I want to return some output and what I'm going to return is that string that we created in our test. The only thing I need to change here is this output okay because this needs to be somewhat dynamic. I'm going to use my F string so at the start of my string this is like a formatted string and then I'm going to just select where it says class and that string class. I'm going to use some two curly braces and let's just put it over here on the right hand side and then I can call the type function and pass in the hours parameter. So if our hours is not either an int or a float of that type then we're going to return this error string and we're going to dynamically print what current type hours is. So let's go ahead and run the tests now and you can see that both of our tests have now passed. So our two tests now test the logic of our function and we're also testing the type of the incoming parameter. Now I just want to open up one of our tests again and you'll see that the big line of code here is this self.assert equals. Remember that just compares two values. We're not limited by this function. Let's jump back over into the Python docs to see what other functions you can use. So over here in the Python docs you can see that we were using this function assert equal where it just compares two values. We've also got assert not equal to, we've got assert true and we've got all these various options. You can see that we also could use that is instance of or is not an instance of. So make sure that you go ahead and play around and explore with some of these assertions. Let's jump back across to Revolet. We're back over here in Revolet and I want to leave you to answer these questions that I'm going to ask you. I've got back up in here our writing tips and I want you to decide whether or not this particular function and these tests decompose our program into a discrete function, a discrete bit of logic, one bit of kind of functionality. Is the function concise and is it descriptive? And does the function have a predictable input and a predictable output? So I'd love for you to consider these points when you go off and you create your tests. Okay let's just hide this from the screen. Now hopefully you've been following along with this demo and we're not done here because we could create more tests. We could create a test to see if ours is negative. Do you want this function to handle negative numbers? You might want to create tests to test different types here so here we tested for strings to make that test but maybe you want to test for lists as well. Basically you want to think about all the possibilities that ours can be and you want to make sure that we get the expected output which is always an integer. What might happen if ours is a float value? Can we have half minutes? Do you want that to be a return result for this particular function as well? So I'll leave that for you to consider. If this video helps you create some unit tests in Replet then be sure to give it a thumbs up because that's going to help the channel grow. And if you enjoyed this content and want to stay in the loop for when more videos come out, be sure to subscribe to the channel and hit that notification bell. You know the drill. But hey, that's it for me. I'm off to go find a wave and I'll catch you in the next one. See you then.