dongpang1232 2018-11-10 11:13
浏览 56
已采纳

在Go中测试生命周期。 是否可以在不重复代码的情况下添加拆解方法?

I started working with Go a month go. I come from java/kotlin background and I would like to understand if it's possible to achieve some of the same stuff that I did in those languages even in Go.

My current problem is this one.

I have a set of integration test cases where I need to initialize some stuff and then clean the resources: a common use case, I believe.

Here's some pseudo code of what I want achieve, if possible:

for each test {
  init resources
  run test {
     init test resources
     execute method under test
     assert
  }
  clean resources
}

At the moment, what I could try, was this approach:

func TestMain(m *testing.M) {
    setup()
    code := m.Run() 
    shutdown()
    os.Exit(code)
}

Which is fine generally speaking if not that it runs at package level. That doesn't give me much control at the moment because I would like to run one of those per test files. (that's what I noticed at least, please let me know if I'm wrong about it)

At the moment what I'm doing is basically run initialization for each test, but that's really a lot of duplicated code:

address, tearDownTestCase := testutils.SetupTestCase(emptyContext, postRouter(login.LoginUser), "/login")
defer tearDownTestCase()
// init test use case data
// run test
// clean use case data

Do you think there is a better approach?

  • 写回答

1条回答 默认 最新

  • duannai1883 2018-11-10 13:27
    关注

    Go does not have the inbuilt functionality to support tear up and down methods. However, there are multiple third-party packages that enable this. Out of all of these, I like the ginkgo package the most. It is very expressive and avoids code duplication.

    A sample test would look like

    var _ = Describe("Book", func() {
        var (
            longBook  Book
            shortBook Book
        )
    
        BeforeEach(func() {
            longBook = Book{
                Title:  "Les Miserables",
                Author: "Victor Hugo",
                Pages:  1488,
            }
    
            shortBook = Book{
                Title:  "Fox In Socks",
                Author: "Dr. Seuss",
                Pages:  24,
            }
        })
    
        Describe("Categorizing book length", func() {
            Context("With more than 300 pages", func() {
                It("should be a novel", func() {
                    Expect(longBook.CategoryByLength()).To(Equal("NOVEL"))
                })
            })
    
            Context("With fewer than 300 pages", func() {
                It("should be a short story", func() {
                    Expect(shortBook.CategoryByLength()).To(Equal("SHORT STORY"))
                })
            })
        })
    })
    

    Similarity there are other lifecycle methods like afterEach, justBeforeEach etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)