How to Write a Good Test
Test functions interact with DB
To test all CRUD operations thoroughly, it is important to create a new testing account first. This ensures that all tests are independent of each other
- First, Connect the DB in the test main file. Eg.
main_test.go
,index.test.js
(?) - Generate a input for the function
- Check test cases
Check list of test cases:
- The returned value from DB (by the function) should not be err
- Returned value should not be empty object
- The argument and the returned value should be matched
- The returned value should contain automatically generated value by DB (eg. ID)
Test case of transactions should be similar
But be careful about concurrency handling (in Golang)
Test APIs
How to mock DB?
- Store data in memory
type Store interface{
GetAccount(id int64) (Account, error)
}
type MemStore struct {
data map[int64]Account
}
func (store * MemStore) GetAccount(id int64) (Account, error) {
return store.data[id], nil
}
2. Use DB stubs
- Eg.
gomock
Write API tests
- Create a tested instance (eg account)
- Init mock store
func TestGetAccountAPI(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockStore := mockdb.NewMockStore(ctrl)
}
- Build stubs
- Create test server and send requests + use recorder to mock response
- check response
Test cases:
- “OK” (Happy case)
- Not Found
- BadRequest
- InternalServerError
- No auth
- Unauthorized User
- 先有好的設計
好的設計
高內聚、低耦合 =>
- (S) Modify A, do not break B
- (O) Feature is stable, consistant
- (L) Change detail but the user can not feel
- (I) The user only uses the interface supplied for him/her
- (D) The user only needs to focus on his/her abstract
在有設計前,先有 Context
瀑布模型本身就是一個有問題的模型,agile 只是為了解決他不是為了對抗它
測試的目的
- 確保功能的正確
- 支持你的 refactor
To Read
- 六角架構
- clean architecture