0
点赞
收藏
分享

微信扫一扫

Go语言单元测试


  • moster.go

package monster

import (
	_"fmt"
	"encoding/json"
	"io/ioutil"
	_"os"
)

type Monster struct{
	Name string
	Age int
	Skill string
}

func (monster *Monster) Store() bool {
	str,_ := json.Marshal(monster)

	filename := "D:/a.txt"

	ioutil.WriteFile(filename, []byte(str), 0777)

    return true
	
}

  • monster_test.go

package monster

import (
	_"fmt"
	"testing"
)

func TestMonster(t *testing.T){
	
	var moster = Monster{
		Name:"张三",
		Age:20,
		Skill:"会员",
	}

	res := moster.Store()

	if res != true {
		t.Fatalf("错误")
	}

	t.Logf("正确")
}

  • 命令行

go test -v .\moster_test.go .\monster.go

  • 结果集



举报

相关推荐

0 条评论