Transcript from the "Unit Testing Solution" Lesson
>> Okay, so let's talk about the solution to our exercise_5a, which was discussing unit tests. And in Part 1, we're writing a test that's gonna validate our Add method that we created in the math.go package. So we look at the math.go package, our function here, function Add, it takes a series of numbers, adds them together, spits out a total, all of which are integers.
[00:00:24] So if we are following the convention in our utils file, we're going to create a new file. And let's call that add_test.go. Cool, add test.go file will want a couple different things. First of all, we needed to match the package that we're working with. And so right now we're in the utils package.
[00:00:45] And the imports that we want here is just that testing library. For the func, we're gonna call it TestAdd and that takes one argument which is the lower case t. And the type is testing.t which is coming from that testing library. And so within here we wanna set up a couple of variables, expected and actual.
[00:01:17] And so you think about working with this Add mathematical method, let’s say, if I called utilits.add and pass in 2 and 2, my actual resulting value should be 4. So then to make it an assertion, excuse me, I wanna say hey, if the actual value does not equal the expected value, I wanna send an error to our testing library.
[00:01:58] We're gonna write some sort of message, Add function does not add up. And I'll give it some information like the, Expected was %d for an integer and the actual is also a %d. And then the two variables we're going to pass through for those values are, expected and actual.
[00:02:32] Down on my terminal, you'll notice that I ran go test and I get this error, no Go files in it toolkit. So I need to navigate to the actual directory where my test lives before I can run Go test. So I navigated to our code directory and then the utils package, which is where our test file lives so far and ran go test.
[00:02:59] We're still seeing those print lines that actually live in our Add function but we're also seeing this PASS and the ok. Indicating that our test is passed. Let's say we change this to 6 just to see the test fail. If I run my test again, you'll see Add function does not add up, we expected a 6, we actually got a 4.
[00:03:21] So it gives you a little bit of information of what your test result it looks like. Let's move that back to a 4 and work on Part 2. So we go back to exercise_5a, Part 2 is to go the opposite direction. We're gonna create a file called strings_test.go and paste in this kind of starter code down at the bottom, which is a test that looks like it's doing some stuff to a string.
[00:03:49] So we're gonna copy this. And in the same utils file, create a New File called strings_test.go, paste in our test. [COUGH] So you'll notice already, I'm getting some linter errors, but let's walk through what's happening in our tests first. So looks like we have a test called TestMakeExcited.
[00:04:14] It takes in that default t, that's coming from our testing library. The expected is a all caps version of a string with an exclamation point. And the actual is a function, capital M, MakeExcited, that takes in some string that has all lowercase letters and no exclamation point. Then we have our assertion block here at the end, where if the actual doesn't match the expected, we wanna print out kinda what those things were.
[00:04:40] If I run go test now, we're going to see undefined: MakeExcited. This makes sense, we haven't written that function yet, so that's what we'll need to do next. So, let's go ahead and make a strings.go file, And this is still living in our package utils. And according to our tests, we need a function that's called MakeExcited.
[00:05:09] We circle back to the test just to confirm, it looks like MakeExcited accepts our single string, so we're gonna say, it accepts some sentence that is a string type. Cool, and then it should give us back, it's supposed to return an actual string. So we know that we need to add the return type here and write some code inside this.
[00:05:36] So right now, it's kind of a trick question, there are a couple of things we need to do in here to make a string go from all lower case letters to all upper case letters. And if you were to dig into kinda the documentation, you might have found that there a package called strings.
[00:05:49] And so we can import the strings package from Go, and then use it. And so in our function here, we can say we want to take our, we're gonna return that formatted version of this string, that's all capitals with the exclamation point. So using our strings package, there's a function called ToUpper that takes a string and it turns it into an uppercase version of that string.
[00:06:19] The last thing that we wanted to do was append on the exclamation point, which we can do with concatenation. And return it at the end of the day. S is short for sentence, so we'll make sure that that matches right there. And then again, because we're exporting this function we need to make sure we have a comment, Transforms a sentence to all caps with an exclamation point.
[00:06:53] Cool? If we run our tests, we'll see that we now have a passing test and implementation to the code, code implementation to the test file.