December 2021
Intermediate to advanced
510 pages
11h 20m
English
Go lets you test your code automatically without requiring external tools or frameworks. You’ll learn more about how to test your command-line applications throughout the book. Right now, let’s write a basic test for the word counter to ensure that it correctly counts the words in the given input.
Create a file called main_test.go in the same directory as your main.go file. Include the following content, which defines a testing function that tests the count function you’ve already defined in the main program:
| | package main |
| | |
| | import ( |
| | "bytes" |
| | "testing" |
| | ) |
| | |
| | // TestCountWords tests the count function set to count words |
| | func TestCountWords(t *testing.T) { |
| | b := bytes.NewBufferString( ... |
Read now
Unlock full access