GoLand没有运行任何测试。

huangapple go评论92阅读模式
英文:

GoLand No Tests Were Run

问题

我有一个正在测试的方法,一切似乎都很好。然而,当我在 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)
	}
}

我做错了什么?

英文:

I have a method that I am testing, and everything seems fine. However, when I run the tests in GoLand, I can see in the output that the tests "PASS" but the test runner says "no tests were run".

Here's the sample method in 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
}

Here's the test in 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)
	}
}

What am I doing wrong?

答案1

得分: 17

AddTwoNumbers中,尝试使用fmt.Println(...fmt.Printf("foo\n"),而不是fmt.Printf(...

你的AddTwoNumbers方法输出中缺少换行符,导致测试执行输出的格式没有将每个测试放在新行中。测试运行程序无法解释已运行的测试。添加换行符可以保持输出的清晰。

英文:

Instead of fmt.Printf(... in AddTwoNumbers try either fmt.Println(... or fmt.Printf("foo\n')

The absence of the newline in the output of your AddTwoNumbers method is is causing the format of the test execution outputs to not have each test in a new line. The test runner is not being able to interpret that a test was run. Adding that newline, keeps a clean output.

答案2

得分: 0

我建议使用go test -v命令来查看测试运行的详细输出。

go test -v

-v选项用于输出详细信息:记录所有运行的测试,并打印所有LogLogf调用的文本,即使测试成功也会打印。

英文:

I would suggest using to check the verbose output of the test run.

go test -v

> -v
Verbose output: log all tests as they are run. Also print all
text from Log and Logf calls even if the test succeeds.

huangapple
  • 本文由 发表于 2021年8月1日 13:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/68607771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定