首页 > 编程知识 正文

stroop色词测验,游戏测试自我介绍

时间:2023-05-03 18:06:26 阅读:108820 作者:3924

1 .安装并使用ginkgogogetgithub.com/onsi/ginkgo/ginkgetgithub.com/onsi/go mega/.所附的库。 下一篇将详细介绍配置环境变量并将其添加到$PATH

2 .准备创建测试文件夹,如2.example。 进入文件夹,运行ginkgo bootstrap命令生成模板文件。 文件名为example_suite_test.go,其中包含执行ginkgo generate example的入口函数。 example可以不写。 缺省值。在测试示例模板文件example_test.go中添加_test后缀是为了区别于当前文件夹中的现有代码,在example_test.go代码中,缺省情况下为当前文件夹

3 .模块常见10个: It、Context、Describe、BeforeEach、AfterEach、JustBeforeEach、BeforeSuite、AfterSuite、By、Fail

It是测试用例的基本单位。 也就是说,对于单个测试用例Context和Describe功能,将一个或多个测试用例归类为BeforeEach也是在每个测试用例运行之前执行的代码。 AfterEach是在每个测试用例运行后执行的代码。 在运行BeforeEach后,JustBeforeEach将在运行测试集之前在运行测试示例前运行BeforeSuite。 也就是说,在运行文件夹中的测试示例之前运行AfterSuite是在运行该测试集之后。 也就是说,在该文件夹中的测试示例运行后,By是打印信息,内容只能是字符串,只有在测试示例失败后才会打印。 常见的调试和定位问题Fail表示该测试示例的执行结果失败。 并且,打印中的信息的另一个Specify与It功能完全相同,It简称4.var _=describe (使用“book”)。 func () var (bookbookerrerrorjsonstring ) beforeeach ) func ) ) JSON=` { ' title ' : ' les Miserables ',' author。 pages ' :1488 } ` } justbeforeeach (func () { book,ERR=NewBookFromjson(JSON ) aftereach ) func ) by ' endoreach func () context、whentheJsonparsessuccesfully、func )、it ) shouldpopulatethefieldscorrectly、func () expect (boot ) 信息技术(shouldnoterror )、 func ) ) expect(err ).notto (have occurred ) ) ) whentheJsonfailstoparse ) ) whentheJsonfailstoparse ) ) func () (title ) : ) lesmiserables ),(author ) : ) victorHugo ),' pages ' :1488 oops } ` (it ) shouldreturnthezero=nil { fail } ' thiscasefailed ' } } } describe { ' extractingtheauthor ' SLAs tname ',func () it (shouldcorrectlyide ) func ) ) {expect ) book.authorlastname ) }.to ) equal五个测试示例由两个Describe来区分,第一个测试示例由两个小类别来区分。 每个It都包含一个测试用例。 两个BeforeEach确保每个BeforeEach仅在当前域中起作用。 执行顺序按同一级别的顺序执行,从不同级别的外部执行到内部级别。 AfterEach这个规则相反。 AfterEach通常用于在测试用例运行完成后进行数据清理。 它还用于最终确定不在var中为变量赋值。 因为每次运行测试用例时都可能更改全局变量的值,这可能会影响以后的测试用例,所以写在BeforeEach中比较好

functestbooks(t*testing.t ) registerfailhandler (fail ) runspecs(t,' Books Suite ' ) }var _=BeforeS

uite(func() { dbRunner = db.NewRunner() err := dbRunner.Start() Expect(err).NotTo(HaveOccurred())dbClient = db.NewClient() err = dbClient.Connect(dbRunner.Address()) Expect(err).NotTo(HaveOccurred()) })var _ = AfterSuite(func() { dbClient.Cleanup() dbRunner.Stop() })

BeforeSuite和AfterSuite写在_suite_test.go文件中,会在所有测试例执行之前和之后执行 如果BeforeSuite执行失败,则这个测试集都不会被执行
Tip:使用^C中断执行时,AfterSuite仍然会被执行,需要再使用一次^C中断

5. 标志

有三个:F、X和P,可以用在Describe、Context、It等任何包含测试例的模块

F含义Focus,使用后表示只执行该模块包含的测试

FDescribe(“outer describe”, func() { It(“A”, func() { … }) It(“B”, func() { … }) })

Tip:当里层和外层都存在Focus时,外层的无效,即下面代码只会执行B测试用例

FDescribe(“outer describe”, func() { It(“A”, func() { … }) FIt(“B”, func() { … }) })

P的含义是Pending,即不执行,用法和F一样,规则的外层的生效X和P的含义一样 还有一个跳过测试例的方式是在代码中加

Skip It(“should do something, if it can”, func() { if !someCondition { Skip(“special condition wasn’t met”) }})

条件满足时,会跳过该测试用例

6. 并发

ginkgo -p 使用默认并发数
ginkgo -nodes=N 自己设定并发数

默认并发数是用的参数runtime.NumCPU()值,即逻辑CPU个数,大于4时,用runtime.NumCPU()-1

并发执行时打印的日志是汇总后经过合并处理再打印的,所以看起来比较规范,每个测试例的内容也都打印在一起,但时不实时,如果需要实时打印,加-stream参数,缺点是每个测试例日志交叉打印

7. goroutine It(“should post to the channel, eventually”, func(done Done) { c := make(chan string, 0)go DoSomething(c) Expect(<-c).To(ContainSubstring("Done!")) close(done) }, 0.2)Ginkgo检测到Done类型参数,就会自动设置超时时间,就是后面那个0.2,单位是秒 8. DesctibeTable用法

有时候很多测试例除了数据部分其他都是相同的,写很多类似的It会很繁琐,于是有Table格式出现

package table_test import ( . “github.com/onsi/ginkgo/extensions/table” . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe(“Math”, func() { DescribeTable(“the > inequality”, func(x int, y int, expected bool) { Expect(x > y).To(Equal(expected)) }, Entry(“x > y”, 1, 0, true), Entry(“x == y”, 0, 0, false), Entry(“x < y”, 0, 1, false), ) })

等同于

package table_test import ( . “github.com/onsi/ginkgo” . “github.com/onsi/gomega” ) var _ = Describe(“Math”, func() { Describe(“the > inequality”, It(“x > y”, func() { Expect(1 > 0).To(Equal(true)) })It("x == y", func() { Expect(0 > 0).To(Equal(false)) }) It("x < y", func() { Expect(0 > 1).To(Equal(false)) }) ) }) 9. 生成JUnit测试报告

一般生成Junit的XML测试报告

func TestFoo(t *testing.T) { RegisterFailHandler(Fail) junitReporter := reporters.NewJUnitReporter("junit.xml")RunSpecsWithDefaultAndCustomReporters(t, "Foo Suite", []Reporter{junitReporter}) } 10. 测试例性能

使用Measure模块

Measure(“it should do something hard efficiently”, func(b Benchmarker) { runtime := b.Time(“runtime”, func() { output := SomethingHard() Expect(output).To(Equal(17)) })Ω(runtime.Seconds()).Should(BeNumerically("<", 0.2), "SomethingHard() shouldn't take too long.") b.RecordValue("disk usage (in MB)", HowMuchDiskSpaceDidYouUse()) }, 10)

该测试例会运行10次,并打印出执行性能数据

• [MEASUREMENT] Suite it should do something hard efficiently Ran 10 samples: runtime: Fastest Time: 0.01s Slowest Time: 0.08s Average Time: 0.05s ± 0.02s disk usage (in MB): Smallest: 3.0 Largest: 5.2 Average: 3.9 ± 0.4

原文链接:https://xiazemin.github.io/MyBlog/golang/2019/11/02/ginkgo.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。