In this article, we’re going to look at how we write test cases in Golang. Here, I will create a file called acct_test.go; which can be run by simply typing go test into the terminal.
The first function tests the creation of a new account. Inside this, we make check the length of the new account slice that is created. We expect it to return a length of 12; if it doesn’t an error is passed to the test handler.
Next, we check the values in the slice. If the first value of the slice doesn’t equal ‘BankName’ it’ll pass an error to the test handler & if the final value isn’t ‘Savings’, it will again throw an error.
Now, we are going to test the save file function. This test:
- Removes a test file (if there is one)
- Creates a new account by calling the new account function
- Saves that account to a file
- Reads that file
- Runs a length check
- Removes the test file
This little test would be enough to prove a file was created & the contents were of the correct length.