ginkgo在windows下的安装使用
ginkgo是GOLANG的一个测试框架
https://github.com/onsi/ginkgo
安装
建一个自己的文件夹,如D:\workspace\go-test-go
在计算机->环境变量中设置GOPATH
打开git bash
执行
go get github.com/onsi/ginkgo/ginkgogo get github.com/onsi/gomega
在git bash中cd到该目录
然后运行下ginkgo help
,看下参数列表
Usage of ginkgo: -a Force rebuilding of packages that are already up-to-date. -afterSuiteHook string Run a command when a suite test run completes -asmflags string Arguments to pass on each go tool asm invocation. -blockprofilerate int Control the detail provided in goroutine blocking profiles by calling runtime.SetBlockProfileRate with the given value. (default 1) -buildmode string Build mode to use. See ‘go help buildmode‘ for more. -compiler string Name of compiler to use, as in runtime.Compiler (gccgo or gc). -compilers int The number of concurrent compilations to run (0 will autodetect) -cover Run tests with coverage analysis, will generate coverage profiles with the package name in the current directory. -covermode string Set the mode for coverage analysis. -coverpkg string Run tests with coverage on the given external modules. -coverprofile string Write a coverage profile to the specified file after all tests have passed. -cpuprofile string Write a CPU profile to the specified file before exiting. -dryRun If set, ginkgo will walk the test hierarchy without actually running anything. Best paired with -v. -failFast If set, ginkgo will stop running a test suite after a failure occurs. -failOnPending If set, ginkgo will mark the test suite as failed if any specs are pending. -flakeAttempts int Make up to this many attempts to run each spec. Please note that if any of the attempts succeed, the suite will not be failed. But any failures will still be recorded. (default 1) -focus string If set, ginkgo will only run specs that match this regular expression. -gccgoflags string Arguments to pass on each gccgo compiler/linker invocation. -gcflags string Arguments to pass on each go tool compile invocation. -installsuffix string A suffix to use in the name of the package installation directory. -keepGoing When true, failures from earlier test suites do not prevent later test suites from running -ldflags string Arguments to pass on each go tool link invocation. -linkshared Link against shared libraries previously created with -buildmode=shared. -memprofile string Write a memory profile to the specified file after all tests have passed. -memprofilerate int Enable more precise (and expensive) memory profiles by setting runtime.MemProfileRate. -msan Enable interoperation with memory sanitizer. -n go test Have go test print the commands but do not run them. -noColor If set, suppress color output in default reporter. -nodes int The number of parallel test nodes to run (default 1) -noisyPendings If set, default reporter will shout about pending tests. (default true) -outputdir string Place output files from profiling in the specified directory. -p Run in parallel with auto-detected number of nodes -pkgdir string install and load all packages from the given dir instead of the usual locations. -progress If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter. -r Find and run test suites under the current directory recursively. -race Run tests with race detection enabled. -randomizeAllSpecs If set, ginkgo will randomize all specs together. By default, ginkgo only randomizes the top level Describe/Context groups. -randomizeSuites When true, Ginkgo will randomize the order in which test suites run -regexScansFilePath If set, ginkgo regex matching also will look at the file path (code location). -seed int The seed used to randomize the spec suite. (default 1471744764) -skip string If set, ginkgo will only run specs that do not match this regular expression. -skipMeasurements If set, ginkgo will skip any measurement specs. -skipPackage string A comma-separated list of package names to be skipped. If any part of the package‘s path matches, that package is ignored. -slowSpecThreshold float (in seconds) Specs that take longer to run than this threshold are flagged as slow by the default reporter. (default 5) -stream stream parallel test output in real time: less coherent, but useful for debugging (default true) -succinct If set, default reporter prints out a very succinct report -tags string A list of build tags to consider satisfied during the build. -toolexec string a program to use to invoke toolchain programs like vet and asm. -trace If set, default reporter prints out the full stack trace when a failure occurs -untilItFails When true, Ginkgo will keep rerunning tests until a failure occurs -v If set, default reporter print out all specs as they begin. -work Print the name of the temporary work directory and do not delete it when exiting. -x go test Have go test print the commands.
生成一个测试套
$ ginkgo bootstrap Generating ginkgo test suite bootstrap for go_test_go in: go_test_go_suite_test.go $ ginkgo generate
查看下生成的文件
$ ls -lrt total 2 -rw-r--r-- 1 opama None 199 Aug 21 02:06 go_test_go_suite_test.go -rw-r--r-- 1 opama None 162 Aug 21 02:07 go_test_go_test.go
可以在一个已有工程为其添加test文件
D:\workspace\hellogo\src\aaa>ginkgo generate Generating ginkgo test for Aaa in: aaa_test.go D:\workspace\hellogo\src\aaa>go test testing: warning: no tests to run PASS ok _/D_/workspace/hellogo/src/aaa 0.309s
用法摘要
ginko的测试代码由一些代码块组成:Describe 代码块描述了被测代码的行为,Context代码块则表示在不同环境下运用这些行为
- 通常情况下,container块(Describe、Context块)内应该是个It块、BeforeEach、AfterEach或变量声明,而不应该放置断言。在container块中初始化了变量,如果其中一个It改变了它的值,后续的It取到的就是改变了的值,此时初始化变量总是应该在BeforeEach块中
var _ = Describe("Node", func() { // Context("Context test",func() { ret := node.GetNode("123") result := comm.GetHttp("456") //fmt.Print(ret); BeforeEach(func() { ret = 666 fmt.Print(ret); }) It("just test suc", func() { ret=200 fmt.Print(ret); Expect(ret).Should(Equal(200)) Expect(result).Should(Equal("456")) }) It("just test fail", func() { fmt.Print(ret); Expect(ret).Should(Equal("123")) Expect(result).Should(Equal("4566")) }) // }) }) $ ginkgo Running Suite: K8sdemo Suite ============================ Random Seed: 1472957391 Will run 2 of 2 specs 666200+666666
- JustBeforeEach在所有BeforeEach块运行完之后并在It块运行之前运行
var _ = Describe("Node", func() { // Context("Context test",func() { ret := node.GetNode("123") result := comm.GetHttp("456") //fmt.Print(ret); BeforeEach(func() { ret = 666 fmt.Print(ret); }) JustBeforeEach(func(){ ret=777 }) It("just test suc", func() { ret=200 fmt.Print(ret); Expect(ret).Should(Equal(200)) Expect(result).Should(Equal("456")) }) It("just test fail", func() { fmt.Print(ret); Expect(ret).Should(Equal("123")) Expect(result).Should(Equal("4566")) }) // }) }) $ ginkgo Running Suite: K8sdemo Suite ============================ Random Seed: 1472957580 Will run 2 of 2 specs 666200+666777
- By用于梳理和标记复杂的流程和步骤,成功了不会显示,失败则将步骤打印出来,通过在Describe、Context、It等加上P或X来标记其为pending
var _ = Describe("Node", func() { // Context("Context test",func() { ret := node.GetNode("123"); result := comm.GetHttp("456"); It("just test suc", func() { Expect(ret).Should(Equal(200)); Expect(result).Should(Equal("456")); }) PIt("just test fail", func() { Expect(ret).Should(Equal("123")); Expect(result).Should(Equal("4566")); }) // }) }) $ ginkgo Running Suite: K8sdemo Suite ============================ Random Seed: 1472956009 Will run 1 of 2 specs + ------------------------------ P [PENDING] Node D:/workspace/k8sdemo/src/node_test.go:27 just test fail D:/workspace/k8sdemo/src/node_test.go:25 ------------------------------ Ran 1 of 2 Specs in 0.000 seconds SUCCESS! -- 1 Passed | 0 Failed | 1 Pending | 0 Skipped PASS
- Focused Specs,只运行F开头的用例,也可以使用–focus=REGEXP, 注意该特若在ci中使用会造成exitcode非0
Running Suite: K8sdemo Suite ============================ Random Seed: 1472956139 Will run 1 of 2 specs S ------------------------------ + Failure [0.001 seconds] Node D:/workspace/k8sdemo/src/node_test.go:27 just test fail [It] D:/workspace/k8sdemo/src/node_test.go:25 Expected <int>: 200 to equal <string>: 123 D:/workspace/k8sdemo/src/node_test.go:23 ------------------------------ Summarizing 1 Failure: [Fail] Node [It] just test fail D:/workspace/k8sdemo/src/node_test.go:23 Ran 1 of 2 Specs in 0.001 seconds FAIL! -- 0 Passed | 1 Failed | 0 Pending | 1 Skipped --- FAIL: TestK8sdemo (0.00s) FAIL Ginkgo ran 1 suite in 2.8093489s Test Suite Failed
- 同步方式的使用:SynchronizedBeforeSuite()传入两个func,第一个必须返回[]byte,第二个必须接受这个[]byte,如一个func是在一个节点起一个server返回endpoint,第二个函数是所有节点client接受endpoint并进行连接,并发时clinet需要等待sever启动再进行连接
同理SynchronizedAfterSuite()传入两个func,第一个func需要全部节点都执行完成,再执行由第一个节点执行第二个func。如在上述的基础上第一个func关闭所有的client,第二个func停止server
ginko对并发异步也能很好的支持测试,在非container块(its,BeforeEach)等增加了一个可选的参数func(done Done),ginkgo检查到done Done参数,遍用goroutine的方式运行body函数,需要通过关闭done channel或发送消息到channel来告诉ginkgo运行完成。如果超时了运行还未结束,ginkgo将把它标记为fail。运行下一个。
- ginkgo常用命令:
并行 gingkgo 快速启动测试 -p 并行 --nodes=Node_total 指定并发节点数 控制 -r 遍历目录 -skipPackage skip目录 输出 --noColor 无颜色 --succinct 精简打印一行 --v 详细打印用例的文本和位置 --noisyPending 对pending的specs也给出信息 --trace 失败打印堆栈信息 --progress 将进入和运行每个部分的日志都打印出来 随机 --seed=SEED focus和skip --skipMeasurements --focus=REGEXP --skip=REGXP coverage -race -cover 失败动作 --failOnPending 有pending的specs就认为是fail --failFast 失败就停止suite的运行 其他 -dryRun 只是遍历,非真实运行 -keepGoing 多个suite有一个失败了仍能继续 -untilItFails 一直运行知道失败 --slowSpecThreshold=TIME_IN_SECONDS 标记运行缓慢的spec 并行 gingkgo 快速启动测试 -p 并行 --nodes=Node_total 指定并发节点数 控制 -r 遍历目录 -skipPackage skip目录 输出 --noColor 无颜色 --succinct 精简打印一行 --v 详细打印用例的文本和位置 --noisyPending 对pending的specs也给出信息 --trace 失败打印堆栈信息 --progress 将进入和运行每个部分的日志都打印出来 随机 --seed=SEED focus和skip --skipMeasurements --focus=REGEXP --skip=REGXP coverage -race -cover 失败动作 --failOnPending 有pending的specs就认为是fail --failFast 失败就停止suite的运行 其他 -dryRun 只是遍历,非真实运行 -keepGoing 多个suite有一个失败了仍能继续 -untilItFails 一直运行知道失败 --slowSpecThreshold=TIME_IN_SECONDS 标记运行缓慢的spec 预编译 ginkgo build xxxx 生成xxxx.test ginkgo -p xxxx.test 将xuint的转为ginkogo ginkgo convert xxxx benchmark tests
- TABLE DRIVEN TESTS
使用Entry、DescrieTable将数据抽离出来传给函数去执行,这个有点像TestNg的DataProvider
时间: 2024-10-13 22:36:04