【问题标题】:GoLand No Tests Were RunGoLand 未运行任何测试
【发布时间】:2021-10-06 23:47:20
【问题描述】:

我有一个正在测试的方法,一切似乎都很好。但是,当我在 GoLand 中运行测试时,我可以在输出中看到测试“通过”但测试运行器说“没有运行测试”。

这是calculator.go中的示例方法

package calculator

import (
    "fmt"
)

type Calculator struct {}

func New() Calculator {
    return Calculator{}
}


func (s *Calculator) AddTwoNumbers(num_one, num_two int) int {
    fmt.Printf("adding")
    return num_one + num_two
}

这是calculator_test.go中的测试:

package calculator

import (
    "fmt"
    "testing"
)

func Test_Calculator_AddTwoNumbers(t *testing.T) {
    // Arrange
    calculator := New()

    // Act
    total := calculator.AddTwoNumbers(1,2)

    // Assert
    if total != 3 {
        msg := fmt.Sprintf("total should have been %d but instead was %d", 3, total)
        t.Error(msg)
    }
}

我做错了什么?

【问题讨论】:

    标签: go testing jetbrains-ide goland go-generate


    【解决方案1】:

    AddTwoNumbers 代替fmt.Printf(... 尝试fmt.Println(...fmt.Printf("foo\n')

    AddTwoNumbers 方法的输出中缺少换行符导致测试执行输出的格式没有每个测试都在新行中。测试运行程序无法解释测试已运行。添加该换行符,保持干净的输出。

    【讨论】:

    • 谢谢@creativityoverflow,它解决了我长期存在的问题。
    • 这正是我的问题,谢谢伙计!
    【解决方案2】:

    我建议使用来检查测试运行的详细输出。

    go test -v
    

    -v 详细输出:在运行时记录所有测试。同时打印所有 即使测试成功,来自 Log 和 Logf 调用的文本。

    【讨论】:

      猜你喜欢
      • 2014-10-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多