dongqiao3927 2017-03-04 18:01
浏览 27

我可以期望(要求)通过测试失败吗?

In other test frameworks that I have used, when writing test helpers, it's nice to be able to automatically test them, i.e. test that they fail as well as pass.

Let me use the following for the helper (in reality it's a lot more complex):

func IsRedirect(t *testing.T, code int) {
    assert.True(t, code >= 300)
    assert.True(t, code < 400)
}

Then sure, I can write:

func TestIsRedirect(t *testing.T) {
    IsRedirect(t, http.StatusSeeOther)
}

But I'd also like to write something like:

func TestNotRedirect(t *testing.T) {
    t.RequireFailure()
    IsRedirect(t, http.StatusOK)
}

Note, I don't really want to write:

func IsRedirect(code int) bool

because the function has more than just one or two conditions that can fail, and I want to be able to assert on the specific failure case.

  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2017-03-04 19:39
    关注

    You could do something like the following:

    func IsRedirect(t *testing.T, code int, b bool) {
        assert.Equal(t, b, code >= 300)
        assert.Equal(t, b, code < 400)
    }
    
    func TestNotRedirect(t *testing.T) {
        IsRedirect(t, http.StatusOK, false)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?