douhuan1937 2013-09-05 13:38 采纳率: 0%
浏览 31
已采纳

单元测试-记录然后失败?

I am used to test drive my code. Now that I am new to Go I am trying to get it right as fast as possible. I am using the testing package in the standard library which seem to be good enough. (I also like that it is not yet another external dependency. We are currently at 2 dependencies overall - compared to any Java- or Ruby project.....) Anyways - it looks like an assert in golang looks like this:

func TestSomething(t *testing.T) {
  something := false
  if something {
    t.Log("Oh noes - something is false")
    t.Fail()      
  }
}

I find this verbose and would like to do it on one line instead:

Assert( something, "Oh noes - something is false" )

or something like that. I hope that I have missed something obvious here. What is the best/idiomatic way to do it in go?

UPDATE: just to clarify. If I was to do something like this:

func AssertTrue(t *testing.T, value bool, message string) {
  if value {
    t.Log(message)
    t.Fail()
  }
}

and then write my test like this

func TestSomething(t *testing.T) {
  something := false
  AssertTrue(t, something, "Oh noes - something is false")
}

then it would not be the go way to do it?

  • 写回答

4条回答 默认 最新

  • dss087358 2013-09-05 14:19
    关注

    There are external packages that can be integrated with the stock testing framework.

    One of them I wrote long ago, gocheck, was intended to sort that kind of use case.

    With it, the test case looks like this, for example:

    func (s *Suite) TestFoo(c *gocheck.C) {
        // If this succeeds the world is doomed.
        c.Assert("line 1
    line 2", gocheck.Equals, "line 3")
    }
    

    You'd run that as usual, with go test, and the failure in that check would be reported as:

    ----------------------------------------------------------------------
    FAIL: foo_test.go:34: Suite.TestFoo
    
    all_test.go:34:
        // If this succeeds the world is doomed.
        c.Assert("line 1
    line 2", gocheck.Equals, "line 3")
    ... obtained string = "" +
    ...     "line 1
    " +
    ...     "line 2"
    ... expected string = "line 3"
    

    Note how the comment right above the code was included in the reported failure.

    There are also a number of other usual features, such as suite and test-specific set up and tear down routines, and so on. Please check out the web page for more details.

    It's well maintained as I and other people use it in a number of active projects, so feel free to join on board, or follow up and check out the other similar projects that suit your taste more appropriately.

    For examples of gocheck use, please have a look at packages such as mgo, goyaml, goamz, pipe, vclock, juju (massive code base), lpad, gozk, goetveld, tomb, etc. Also gocheck, manages to test itself. It was quite fun to bootstrap that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?