This course has been updated! We now recommend you take the Basics of Go course.

Check out a free preview of the full Go for JavaScript Developers course:
The "Unit Testing Exercise" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to write a new unit test to ensure a preexisting function runs as expected, and then to use test-driven development (TDD) to write code to make a preexisting test pass.

Get Unlimited Access Now

Transcript from the "Unit Testing Exercise" Lesson

>> So for exercise 5A, we're gonna practice unit testing. So far we've added a utils package in a math.go file. And then in that file, we wrote a really simple add method that just adds a couple numbers together. So let's go look at exercise 5A .markdown and talk about what we're gonna do for this exercise.

[00:00:23] So there's a couple different parts to 5A. The first one is kinda similar to what you'd expect. We just wrote the add method in our utils package. So I'd like everyone to add an appropriately named test file and then write a test for that particular function. And then run a test with run test to make sure that it's passing.

[00:00:46] Part two is going to kinda go the opposite direction. We're gonna do some TDD or test driven development, to write a function that makes test implementation pass. So in that same utils directory, we don't have a strings test go file yet, but you're gonna wanna create one. You'll copy the following code that lives here into that file, which is just a test for code that doesn't exist yet.

[00:01:15] And then implement the necessary code to make that test pass. So the code that lives here, you can create a file called stringstest.go, just in your utils directory. If you run the test now, it's going to fail and print out something about things don't exist yet. And so you'll take a couple minutes to implement the code in a presumably strings.go file that'll make this test passed.