当前位置:网站首页>go单元测试实例;文件读写;序列化
go单元测试实例;文件读写;序列化
2022-06-11 04:44:00 【水是睡着的冰】
store.go
package monster
import (
"encoding/json"
"io/fs"
"io/ioutil"
"log"
)
var f = "d:/monster.json"
type Monster struct {
Name string
Age int
Skill string
}
func (m *Monster) Store() error {
ms, err := json.Marshal(m)
if err != nil {
log.Fatalf("mashal error:%v", err)
return err
}
if err := ioutil.WriteFile(f, ms, fs.ModePerm); err != nil {
log.Fatalf("Write file error:%v", err)
return err
}
return err
}
func (m *Monster) ReStore() *Monster {
f, err := ioutil.ReadFile(f)
if err != nil {
log.Fatalf("ReadFile error:%v", err)
return nil
}
monster := &Monster{}
err = json.Unmarshal(f, monster)
if err != nil {
log.Fatalf("Unmarshal error:%v", err)
return nil
}
return monster
}
store_test.go
package monster_test
import (
"lch-tests/monster"
"testing"
)
func TestStore(t *testing.T) {
monster := &monster.Monster{
Name: "konglong",
Age: 100000,
Skill: "kill human",
}
monster.Store()
mStored := monster.ReStore()
if mStored.Name != "konglong" {
t.Fatalf("不是预期效果! 单元测试不通过!")
} else {
t.Logf("well done!")
}
}
运行结果
go test -v -test.run TestStore
=== RUN TestStore
store_test.go:19: well done!
--- PASS: TestStore (0.00s)
PASS
ok my-tests/monster_test 0.228s
边栏推荐
- 强大新UI装逼神器微信小程序源码+多模板支持多种流量主模式
- [Transformer]MViTv1:Multiscale Vision Transformers
- 国际琪貨:做正大主帐户风险有那些
- An adaptive chat site - anonymous online chat room PHP source code
- Meedu knowledge payment solution v4.5.4 source code
- 碳路先行,华为数字能源为广西绿色发展注入新动能
- 新库上线 | CnOpenData不可移动文物数据
- 正大国际;做主帐户需要了解什么
- Google Code Coverage best practices
- Qt生成二维码图片方法
猜你喜欢

MindManager22专业版思维导图工具

Mindmanager22 professional mind mapping tool

The third small class discussion on the fundamentals of information and communication

Exness: Liquidity Series - order Block, Unbalanced (II)

ACTS:高效的测试设计(并赠送一个优秀的测试设计工具)

Commissioning experience and reliability design of brushless motor

图的最短路径问题 详细分解版

Huawei equipment is configured with cross domain virtual private network

Unity advanced backpack system

Unity item model rotating display
随机推荐
codesys 获取系统时间
Collation of construction data of Meizhou plant tissue culture laboratory
Leetcode question brushing series - mode 2 (datastructure linked list) - 206:reverse linked list
Leetcode question brushing series - mode 2 (datastructure linked list) - 24 (m): swap nodes in pairs exchange nodes in the linked list
Powerful new UI installation force artifact wechat applet source code + multiple templates support multiple traffic main modes
Guanghetong advanced the fm160-cn project sample of the first 3GPP R16 industrial 5g module customized for China
芯源cw32f030c8t6用keil5编译时出现的问题
Batch naming picture names
C语言试题三(程序选择题进阶_含知识点详解)
Sharing of precautions for the construction of dioxin laboratory in Meizhou
[markdown syntax advanced] make your blog more exciting (III: common icon templates)
智慧工地怎样做到数字化转型?
USB to 232 to TTL overview
Commissioning experience and reliability design of brushless motor
Minor problems encountered in installing the deep learning environment -- the jupyter service is busy
QT method for generating QR code pictures
国际琪貨:做正大主帐户风险有那些
Leetcode classic guide
Chia Tai International: anyone who really invests in qihuo should know
关于串口波特率的的记录