ginkgo: ginkgo配合gomega demo

官方文檔參考:
http://onsi.github.io/ginkgo/
https://onsi.github.io/gomega/#matchjsonjson-interface
說明
gomega 裏面有很多斷言判斷的方法, 本文就是參考 官方文檔實踐的, 入門的同學可以參考以下代碼

代碼
test.go

package test

type Farm struct {
	Animal []Animal
}
type Animal struct {
	Name string
}

func (a *Animal)NewAnimal(name string)(*Animal)  {
	a.Name=name
	return a
}
func (f *Farm) NewFarm(name[] Animal) (*Farm) {
	f.Animal=name
    return f
}
func (f *Farm) HasCow()(status bool)  {
	for _,v:=range(f.Animal) {
		if v.Name == "cow" {
			return true
		}
	}
	return false
}

test_suite_test.go

package test

import (
	"github.com/onsi/ginkgo"
	"github.com/onsi/ginkgo/reporters"
	"github.com/onsi/gomega"
	"k8s.io/klog"
	"k8s.io/kubernetes/test/e2e/framework"
	"k8s.io/kubernetes/test/e2e/framework/ginkgowrapper"
	"os"
	"path"
	"testing"
)

func TestDemo(t *testing.T) {
/*	RegisterFailHandler(Fail)
	RunSpecs(t, "Felix Suite")*/
	gomega.RegisterFailHandler(ginkgowrapper.Fail)
	var r []ginkgo.Reporter
	if framework.TestContext.ReportDir != "" {
		if err := os.MkdirAll(framework.TestContext.ReportDir, 0755); err != nil {
			klog.Errorf("Failed creating report directory: %v", err)
		} else {
			r = append(r, reporters.NewJUnitReporter(path.Join(framework.TestContext.ReportDir, "junit.xml")))
		}
	}
	ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "EKS e2e suite", r)
}

test_test.go

package test_test

import (
	"git.code.oa.com/TKE_test/TKETest/testcase/test"
	"github.com/onsi/ginkgo"
	"github.com/onsi/gomega"
)

var _ =ginkgo.Describe("eks case", func() {
	var animal []test.Animal
	animal= append(animal, test.Animal{
		Name: "cow",
	})
	animal= append(animal, test.Animal{
		Name: "hourse",
	})
	animal= append(animal, test.Animal{
		Name: "rabbit",
	})
	var farm test.Farm
	farm.NewFarm(animal)

	ginkgo.It("case1", func() {
		ginkgo.By("case1")
		gomega.Ω(farm.HasCow()).Should(gomega.Equal(true),"農場有奶牛")
		gomega.Ω(farm.HasCow()).Should(gomega.BeTrue(),"農場有奶牛")
	})
/*	ginkgo.It("farmwork", func() {
		str:=framework.GetGPUDevicePluginImage()
		fmt.Println(str)
	})*/
})





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章