golang best practice 之 dev测试

fstMoon · · 807 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

基本过程

  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 With fx 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替换,避免在更深的层次进行替换时的繁琐操作。


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:fstMoon

查看原文:golang best practice 之 dev测试

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

807 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传