golang best practice 之 dev测试

栏目: Go · 发布时间: 7年前

内容简介:Unit Test -> Integrate Test -> Dev, from fake to realRun unit test by hand or script with go test.Write Unit Test Withfx support to inject some mock component, example:

基本过程

  1. coding in local IDE,write unit test,
  2. add & run integrate test(tradeoff between自动化与手动,如果自动化写的少,多动手)
  3. deploy to dev environment
  4. deploy to qa or production environment. not be discussed on this page

Requirement

Environment in logic

  1. IDE environment for develop effectively. Should support debug interactively with ide breakpoint.
  2. Integrate Test environment. For run integrate by hand or automatically.
  3. Local Environment.
  4. Dev environment.
    And QA environment、Prod environment not maintained by dev

Environment in implement

Realness

Unit Test -> Integrate Test -> Dev, from fake to real

Implement

Run unit test by hand or script with go test.

Unit Test

Write Unit Test Withfx support to inject some mock component, example:

func TestFxPopluate(t *testing.T) {
 // Some external module that provides a user name.
 type Username string
 UserModule := fx.Provide(func() Username { return "john" })

 var user Username
 app := fxtest.New(
  t,
  UserModule,
  fx.Populate(&user),
 )
 app.RequireStart().RequireStop()
 t.Log(user)
}

Integrate Test

use httptest library, example

func TestHttpTestApi(t *testing.T) {
 InitTest()
 var r *gin.Engine
 app := fxtest.New(t, logger.Module, database.Module, cache.Module, config.Module,
  dao.Module, handler.ModuleWithDep, router.Module, fx.Populate(&r))
 app.RequireStart().RequireStop()
 // create request
 req, err := http.NewRequest("GET", "/api/v3/test/ping", nil)
 if err != nil {
  t.Fatal(err)
 }

 // response checker
 rr := httptest.NewRecorder()

 r.ServeHTTP(rr, req)

 if status := rr.Code; status != http.StatusForbidden {
  t.Errorf("handler returned wrong status code: got %v want %v",
   status, http.StatusOK)
 }

 return
 /*
  expected := `{"message": "pong"}`
  if rr.Body.String() != expected {
   t.Errorf("handler returned unexpected body: got %v want %v",
    rr.Body.String(), expected)
  }
 */
}

Distinguish env by shell env value or cmd flag or both, supported by config library

IDE env、Integrate Test env、Local env can use same config、same environment, but with some difference, so use different flag. Different Requirement

  • IDE env require run in IDE, so more mock component. Actually one of server、fe run separately in IDE,other run in docker
  • Integrate Test should clean db and other state before every run. Integrate can be run in any one of IDE、local、dev、QA env with clean db、state
  • Local env require keep state.

Use docker to implement this.

Env define: ["ide", "local", "dev"], and ["qa", "prod"]

Use shell env、cmd flags to determine which env to run, as to load different config and component.

monkey patch

https://github.com/bouk/monkey

可以极大地简化使用依赖注入时的mock替换,避免在更深的层次进行替换时的繁琐操作。


以上所述就是小编给大家介绍的《golang best practice 之 dev测试》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

移动风暴

移动风暴

[美]弗雷德·沃格尔斯坦 / 朱邦芊 / 中信出版社 / 2014-1-1 / 39

也许,除了伟大的乔布斯,每一位奋力改变世界的硅谷英雄,都值得我们肃然起敬。苹果与谷歌十年博弈,关于这场移动平台战争的报道早已铺天盖地,而这是第一次,我们能听到幕后工程师的真实声音。两大科技巨人用智能手机和平板电脑颠覆了电脑产业。它们位处变革的中心,凭借各自的经营哲学、魅力领袖和商业敏感度,把竞争变成了残酷对决。商业记者沃格尔斯坦报道这场对抗已逾十载,在《移动风暴》中,他带领我们来到一间间办公室和会......一起来看看 《移动风暴》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试