当前位置: 首页 > 工具软件 > Ginkgo > 使用案例 >

ginkgo: ginkgo配合gomega demo

魏俊茂
2023-12-01

官方文档参考:
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)
	})*/
})





 类似资料: