dsfdfdfd6576578 2019-05-03 03:42
浏览 86

go测试用例不在主程序包中运行

I'm trying to write a simple test to get a better understanding of golang testing but the test case doesn't seem to execute and I'm expecting it to fail.

In my main.go I have:

package main

import "fmt"

func main() {

    fmt.Println("run")

}

func twoSum(nums []int, target int) []int {
    lookup := make(map[int]int)
    for i, n := range nums {
        c := target - n
        if j, ok := lookup[c]; ok {
            return []int{j, i}
        }
        lookup[n] = i
    }
    return []int{}

}

and then in my main_test.go I have this:

package main

import (
    "reflect"
    "testing"
)

var twoSumsCases = []struct{
    input []int
    target int
    expected []int

} {
    {
         []int{2,7,11,15},
         9,
         []int{0,3},

    },
}

func TesttwoSum(t *testing.T) {

    for _, tc := range twoSumsCases {
        actual := twoSum(tc.input, tc.target)

        eq := reflect.DeepEqual(actual, tc.expected)

        if eq {
            t.Log("expected: ", tc.expected, " actual: ", actual)
        } else {
            t.Error("expected: ", tc.expected, " actual: ", actual)

        }
    }
}

then when I run go test -v...it tells me that testing: warning: no tests to run. I looked at this as an example: https://blog.alexellis.io/golang-writing-unit-tests/...and I think i got everything I need to but not sure why the test isn't executing.

  • 写回答

2条回答 默认 最新

  • douchi0471 2019-05-03 04:08
    关注

    Change the name of the test function to TestTwoSum. This name matches the pattern described in first paragraph of the testing package documentation:

    ... It is intended to be used in concert with the “go test” command, which automates execution of any function of the form

    func TestXxx(*testing.T)
    

    where Xxx does not start with a lowercase letter. ...

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题