前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GoLand No Tests Were Run : 不能使用 fmt.Printf() <BUG>

GoLand No Tests Were Run : 不能使用 fmt.Printf() <BUG>

作者头像
一个会写诗的程序员
发布2022-06-29 13:40:01
7230
发布2022-06-29 13:40:01
举报
文章被收录于专栏:一个会写诗的程序员的博客

问题描述:

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)
    }
}

问题解决:

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.

https://stackoverflow.com/questions/68607771/goland-no-tests-were-run

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档