通过测试学习 Go 编程

时间:2021-08-30 09:10:39   收藏:0   阅读:16

通过测试学习 Go 编程

TDD 简介

TDD是测试驱动开发(Test-Driven Development)的英文简称,是敏捷开发中的一项核心实践和技术,也是一种设计方法论。TDD的原理是在开发功能代码之前,先编写单元测试用例代码,测试代码确定需要编写什么产品代码。TDD虽是敏捷方法的核心实践,但不只适用于XP(Extreme Programming),同样可以适用于其他开发方法和过程。

TDD 三条规则

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  • 除非为了使一个失败的unit test通过,否则不允许编写任何产品代码
  1. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  • 在一个单元测试中只允许编写刚好能够导致失败的内容(编译错误也算失败)
  1. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
  • 只允许编写刚好能够使一个失败的unit test通过的产品代码

Go 编写测试规则

规则

示例:

package main

import "testing"

func TestHello(t *testing.T) {
    got := Hello()
    want := "Hello, world"

    if got != want {
        t.Errorf("got %q want %q", got, want)
    }
}

Go 测试函数运行

用法

usage: go test [build/test flags] [packages] [build/test flags & test binary flags]

flags 介绍

[^ 1 ]: 不太理解这-o flag 使用
[^ 2 ]: 支持运行指定的基准测试,例如:-bench=正则表达式 尚不清楚如何使用
[^ 3 ]: 如何指定时间尚不清楚

原文:https://www.cnblogs.com/python-beginner/p/15201789.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!