This course has been updated! We now recommend you take the Basics of Go course.
Transcript from the "Testing" Lesson
[00:00:00]
>> So for the next section before we get in to an exercise. I wanna talk super briefly about Unit Testing. So testing in Go, Go does have a built in library for testing which I find it incredibly useful. If you're like me, the series of small teams I have worked on, all have decided to use different testing frameworks for different reason.
[00:00:18] Some people want Cyprus, Mocha Chai, when in the mood for that day with Go, it's a built in testing framework that makes testing kind of a little bit more painless. So to write a unit test, you're gonna have a couple different things. First of all, you're gonna be working with some sort of goFile.
[00:00:35] So we just created that map goFile. In the example on the screen, you're looking at an average.go file. Let's say this file has a couple utility functions that calculate the average like we've been doing in some of the examples. In order to test that file you'll need to call the file name average_test.
[00:00:49] So you typically do whatever that file is your testing_test.go. The test file does need to live in the same directory as the go code that you're testing. And then you'll write a test that looks similar to the example on the board which we'll work through together. And then to actually run that code at the end of the day you'd use Go test.
[00:01:10] So a couple things that are happening in the test file itself, the package name should match the actual package that you're working with. So for in that utils file testing the math.go, it'd be package utils. And you can import the testing library directly from the built-in source code from Go.
[00:01:27] The only func we need in this file is a test function itself. And the convention here, is to use capital T test and then the capital name of whatever function or file you're testing. So here we're gonna do test average, and it takes one argument. The argument is represented here by T, and then a pointer value to testing.T.
[00:01:46] And the pointer values, we're gonna cover in the next lesson, but it indicates that this variable is something we can modify. So the next couple of lines, 10 and 11, we're settings these values ourselves. So in Mocha Chai, you have kind of an assertion library where this is the structure you need your assertion to match, to then get the results from your test.
[00:02:06] And in Go we get to kind of have a little bit more control over what that looks like. So I'm setting two different variables. The first is what I expect to have happen out of the function. And the second is what's actually happening when I call utils.average and pass some values into it.
[00:02:19] From 13 to 15, I'm saying hey, if the actual value doesn't match the expected throw an error modify that T variable, and I want you to send the error, hey, the average was incorrect. I expected this one thing, I got this actual thing, and then passing in those verbs like we did with this string formatting earlier today.