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)
	})*/
})





좋은 웹페이지 즐겨찾기