首页 > 其他分享 >vscode 无法调试 golang testify suite 中的单个 test 的解决办法

vscode 无法调试 golang testify suite 中的单个 test 的解决办法

时间:2024-05-19 23:51:41浏览次数:16  
标签:vscode VariableThatShouldStartAtFive testify golang go test suite

目录

问题描述

对于如下这样简单的测试文件:

package main

// Basic imports
import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)

var assertObj *assert.Assertions

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type ExampleTestSuite struct {
	suite.Suite
	VariableThatShouldStartAtFive int
}

// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
	suite.VariableThatShouldStartAtFive = 5
	assertObj = assert.New(suite.T())
}

// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
	assertObj.Equal(5, suite.VariableThatShouldStartAtFive)
	suite.Equal(5, suite.VariableThatShouldStartAtFive)
}

func (suite *ExampleTestSuite) TestExample2() {
	assertObj.NotEqual(51, suite.VariableThatShouldStartAtFive)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
	suite.Run(t, new(ExampleTestSuite))
}

点击 TestSuite 的某一个 Test* 方法上的 debug test,就会只调试这单个 test,这是符合预期的。

但是在一个复杂项目中,如果 TestSuite 对象的 Test* 方法分布于多个 *_test.go 文件中,这时想要单独执行某一个 Test*,就会出现 testing: warning: no tests to run 这样的错误提示:

网上的讨论

2022 年 4 月,github 上就有同样问题的讨论:
cannot debug single test in VS Code #1177
Failure to debug a suite test that is in a different file than the caller test #2414

一个回答是在 vscode 中使用 Go Nightly 插件来代替 Go 插件,收货 4 个点赞,看样子是可行的。

但是实际测试发现还是不行,替换插件后,重启了 vscode,依然不行。

最终的解决办法

参考这个回答:

.vscode/launch.json 中进行如下配置:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug TestMethodOne",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${workspaceFolder}/pkg/service/service_test.go",
            "args": ["-test.run", "MyTestSuite/TestMethodOne"],
        },
        {
            "name": "Debug TestMethodTwo",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${workspaceFolder}/pkg/service/service_test.go",
            "args": ["-test.run", "MyTestSuite/TestMethodTwo"],
        }
    ]
}

然后就可以在 vscode 的 Debug 页面中成功调试单个 test 实例了

标签:vscode,VariableThatShouldStartAtFive,testify,golang,go,test,suite
From: https://www.cnblogs.com/dongling/p/18200976

相关文章

  • vscode配置
    vscode配置设置自定义指令选择vue.json使用V就会弹出快捷代码{ "Printtoconsole":{ "prefix":"v", "body":[ "<scriptsetuplang=\"ts\">", "</script>", "<templa......
  • stm32cubemx+cmake vscode开发
    stm32cubemx在2.17之后可以新建cmake工程文件,然后就可以在vscode中无痛编译单片机程序,可以再配合openocd等编译调试下载1.准备1.1stm32cubemx1.2vscode1.3arm-none-eabi-gcc2.cubemx生成cmake工程文件2.1利用__weak函数实现cubemx代码与用户代码分离2.2利用宏定义......
  • 解决VSCode中Debug和运行路径不一致的
    哈喽,大家好,我是木头左!背景介绍在VisualStudioCode(简称VSCode)中进行开发时,经常需要使用到调试(Debug)功能。然而,有时候会发现,当尝试调试程序时,程序的运行路径与预期不符。这通常会导致程序无法正确读取文件或访问资源,从而影响调试过程。为了解决这个问题,可以在launch.json文件......
  • vscode配置格式化工具Prettier
    1,安装Prettier插件2,设置VsCode默认格式化工具为Prettier.1)通过菜单打开设置页面:file/preferences/settings2)设置默认格式化工具为Prettier3,配置默认参数4,保存文件自动格式化 5, 在项目中配置代码规范1)打开官网: Prettier中文网·Prettier是......
  • 连接池设计与实现一——以Golang Http1.1为例
    0.前言连接池是一个非常重要的开发思想,如httpclient会构建连接池复用底层TCP连接,使用database/sql的使用也会有连接池的配置。那么代码底层是如何实现连接池的呢?这篇文档将以Golang语言为基础,分析http1.1连接池底层实现★注意:我们仅仅关注连接池设计思想、以及关键源码解读,......
  • Linux/Golang/glibC系统调用
    Linux/Golang/glibC系统调用本文主要通过分析Linux环境下Golang的系统调用,以此阐明整个流程有时候涉略过多,反而遭到质疑~,写点文章证明自己实力也好Golang系统调用找个函数来分析https://pkg.go.dev/os/exec#Cmd.Wait源码文件在src/os目录下的:exec.go->exec_unix.go->......
  • VSCode安装vue3插件
    1.以前的volar已经弃用了。2.最近vue插件 3.安装好插件后,在vscode中创建项目可能会报错。解决方法:1.使用window+r调出cmd,运行node-v,npm-v都没问题。那么尝试以管理员方式运行vscode,如果还是不行,重启一下电脑。2.网络原因,网络波动或者网比较差的时候导致下载丢包,把node......
  • Golang初学:获取程序内存使用情况,std runtime
    goversiongo1.22.1windows/amd64Windows11+amd64x86_64x86_64GNU/Linux--- 序章本文介绍golang程序占用内存的监控:使用stdruntime的ReadMemStats函数。 ReadMemStats函数https://pkg.go.dev/[email protected]//函数funcReadMemStats(m*MemStats......
  • vscode vim
    easymotionfindchar:<leader><leader>sfindcharforward/backwark:<leader><leader>f/Ffindwordstart/end<leader><leader>w/bfindline<leader><leader>j/kfindwith/:<leader><leader>/......
  • vscode launch
    {"version":"0.2.0","configurations":[{"name":"C/C++Runner:DebugSession","type":"cppdbg","request":"launch","args&......