go test进行单元测试时,出现undefined方法或者command-line-arguments [build failed]解决方案

本文介绍了在Go语言中使用gotest执行单个测试文件时遇到的引用错误,分析了原因——代码包冲突,并提供了通过在命令行中添加依赖文件来解决的方法。重点在于如何确保正确编译和执行测试,避免因包引用导致的编译失败。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

很多人记录过怎么执行Go test单个文件或者单个函数,但是要么对执行单文件用例存在函数或变量引用的场景避而不谈,要么提示调用了其它文件中的模块会报错。其实了解了go test命令的机制之后,这个问题就迎刃而解了。

也是一个同事向我求助我才知道有这样一个问题存在,因为场景特殊他只想执行我们的测试代码其中一个文件中的测试代码,因为调用的信息都在同一目录(package)下,原代码执行是不存在引用问题的,直接执行go test也是可以运行的,但是单独运行其中一个文件时,出现了下面的错误:

bingo@Mac Interface_test$ go test -v getinfo_test.go

command-line-arguments

./getinfo_test.go:34:23: undefined: touch
./getinfo_test.go:35:23: undefined: verify

FAIL command-line-arguments [build failed]
这也许是很多数人会遇到的一个错误,搜索引擎上没有找到相关的信息,有的人做了相关的记录来提示后来人:执行单文件存在引用时会报错。

这可能也是使用golang 做测试的弊端,因为发展时间不长,没有像Python或者Java那样成熟的社区;废话不多说,我们一起来看看这个问题为什么会发生。

根本原因

其实从看看上面的这段提示:build failed,构建失败,我们应该就能看出一下信息。go test与其他的指定源码文件进行编译或运行的命令程序一样(参考:go run和go build),会为指定的源码文件生成一个虚拟代码包——“command-line-arguments”,对于运行这次测试的命令程序来说,测试源码文件getinfo_test.go是属于代码包“command-line-arguments”的,可是它引用了其他包中的数据并不属于代码包“command-line-arguments”,编译不通过,错误自然发生了。

解决方法

解决

知道了原因之后,解决的方法就出来了,执行命令时加入这个测试文件需要引用的源码文件,在命令行后方的文件都会被加载到command-line-arguments中进行编译。示例如下:

bingo@Mac Interface_test$ go test -v getinfo_test.go  lib.go 
ok      command-line-arguments  0.008s

多级引用或多个引用包的情况

如果对多个包存在引用关系,或者引用的包对当前目录的其他文件存在引用,也都是一样的方法,把他们加在命令后方即可。

下面这个是一个多引用包的实例:
多个引用包需要命令分别导入

总结

测试单个文件,一定要带上被测试的原文件,如果原文件有其他引用,也需一并带上。

文章来自 【Golang】解决Go test执行单个测试文件提示未定义问题

注意:在执行go test命令进行单元测试的时候,需要将待测试文件和引用的源文件都带上,并且将引用的源文件放在待测试文件前面。例如,在./services目录下有Live.go源文件,现在针对该文件中的函数进行单元测试,单测方法名为Test_LiveList,放在./services/Live_test.go文件中,执行单元测试命令如下:

$ go test -v ./services/Live.go ./services/Live_test.go 
=== RUN   Test_LiveList
......

参考

【Golang】解决Go test执行单个测试文件提示未定义问题
go test 执行单元测试文件时 提示未定义的问题
go test命令(Go语言测试命令)完全攻略
使用Go Test测试单个文件和单个方法

#include "ns3/aodv-module.h" #include "ns3/core-module.h" #include "ns3/internet-module.h" #include "ns3/mobility-module.h" #include "ns3/network-module.h" #include "ns3/ping-helper.h" #include "ns3/point-to-point-module.h" #include "ns3/yans-wifi-helper.h" #include "ns3/netanim-module.h" #include <cmath> #include <iostream> using namespace ns3; class AodvExample { public: AodvExample(); /** ◦ \brief Configure script parameters ◦ \param argc is the command line argument count ◦ \param argv is the command line arguments ◦ \return true on successful configuration */ bool Configure(int argc, char** argv); /// Run simulation void Run(); /** ◦ Report results ◦ \param os the output stream */ void Report(std::ostream& os); private: // parameters /// Number of nodes uint32_t size; /// Distance between nodes, meters double step; /// Simulation time, seconds double totalTime; /// Write per-device PCAP traces if true bool pcap; /// Print routes if true bool printRoutes; // network /// nodes used in the example NodeContainer nodes; /// devices used in the example NetDeviceContainer devices; /// interfaces used in the example Ipv4InterfaceContainer interfaces; private: /// Create the nodes void CreateNodes(); /// Create the devices void CreateDevices(); /// Create the network void InstallInternetStack(); /// Create the simulation applications void InstallApplications(); }; int main(int argc, char** argv) { AodvExample test; if (!test.Configure(argc, argv)) { NS_FATAL_ERROR("Configuration failed. Aborted."); } test.Run(); test.Report(std::cout); return 0; } //----------------------------------------------------------------------------- AodvExample::AodvExample() : size(100), step(50), totalTime(100), pcap(true), printRoutes(true) { }
最新发布
03-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值