当前位置: > > > > 在单元测试中模拟/伪造/替换硬件相关功能
在单元测试中模拟/伪造/替换硬件相关功能
来源:stackoverflow
2024-04-23 22:06:37
0浏览
收藏
本篇文章向大家介绍《在单元测试中模拟/伪造/替换硬件相关功能》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。
问题内容
我目前在一个文件中有以下函数:
func pinexported(pin int) bool {
pinpath := fmt.sprintf("/sys/class/gpio/gpio%d", pin)
if file, err := os.stat(pinpath); err == nil && len(file.name()) > 0 {
return true
}
return false
}
同一文件中的另一个代码部分使用上述函数,如下所示:
func isgpiopinexported(gpiopin int) bool {
exported := pinexported(gpiopin)
for !exported && (timeout < timeoutforpinexportinmilliseconds) {
timeout++
time.sleep(1 * time.millisecond)
exported = pinexported(gpiopin)
}
...
所以现在我正在寻找一种优雅的方法来模拟/替换单元测试中的上述 pinexported 函数,以测试 isgpiopinexported 内部的逻辑,因为函数 pinexported 依赖于硬件(raspberry pi)。
一种解决方案可能是将 pinexported 函数作为 isgpiopinexported 的参数
因此定义一个这样的函数类型:
type pinexported func(int) int
这意味着我必须像这样定义 isgpiopinexported :
isGpioPinExported(pinExported pinExported, gpioPin int) bool {
exported := pinExported(gpioPin)
for !exported && (timeOut < timeOutForPinExportInMilliseconds) {
...
}
..
}
现在我可以编写单元测试并定义模拟/假 pinexported ,没有任何问题。到目前为止,一切都很好。但我有大约五六个这样的函数,这意味着它将导致将五六个补充参数放入像 isgpiopinexported 这样的函数中,这是完全错误的。除此之外,问题是如果没有在测试中运行,我在哪里可以定义使用的默认实现?
解决方案
因此,根据 mkopriva 的建议,我创建了一个如下所示的界面(现在具有三个函数来查看其实际工作原理):
type raspberry interface {
ispinexported(gpiopin int) bool
valueexist(gpiopin int) bool
directionexist(gpiopin int) bool
}
进一步定义了一个结构体来实现真实硬件(raspberry):
type rasberry3plus struct {
}
func (raspberry rasberry3plus) valueexist(gpiopin int) bool {
pinpath := fmt.sprintf("%s%d/value", sysclassgpiopin, gpiopin)
if file, err := os.stat(pinpath); err == nil && len(file.name()) > 0 {
return true
}
return false
}
func (raspberry rasberry3plus) directionexist(gpiopin int) bool {
pinpath := fmt.sprintf("%s%d/direction", sysclassgpiopin, gpiopin)
if file, err := os.stat(pinpath); err == nil && len(file.name()) > 0 {
return true
}
return false
}
func (raspberry rasberry3plus) ispinexported(gpiopin int) bool {
pinpath := fmt.sprintf("%s%d", sysclassgpiopin, gpiopin)
if file, err := os.stat(pinpath); err == nil && len(file.name()) > 0 {
return true
}
return false
}
使用上述函数的函数 isgpiopinexported 现在看起来像这样(这只是一个示例实现,用于了解模拟测试如何工作):
func isgpiopinexported(raspberry raspberry, gpiopin int) bool {
pinexported := raspberry.ispinexported(gpiopin)
valueexist := raspberry.valueexist(gpiopin)
directionexist := raspberry.directionexist(gpiopin)
return valueexist && directionexist && pinexported
}
现在测试看起来像这样。首先,我必须定义一个类型(顺便说一句:我决定使用 mock):
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type mockraspberry struct {
mock.mock
}
func (raspmock mockraspberry) ispinexported(gpiopin int) bool {
args := raspmock.called(gpiopin)
return args.bool(0)
}
func (raspmock mockraspberry) valueexist(gpiopin int) bool {
args := raspmock.called(gpiopin)
return args.bool(0)
}
func (raspmock mockraspberry) directionexist(gpiopin int) bool {
args := raspmock.called(gpiopin)
return args.bool(0)
}
func test_valuetrue_directionexisttrue(t *testing.t) {
testobj := new(mockraspberry)
testobj.on("ispinexported", 5).return(false)
testobj.on("valueexist", 5).return(true)
testobj.on("directionexist", 5).return(true)
exported := isgpiopinexported(testobj, 5)
assert.equal(t, false, exported)
}
现在可以很简单地使用适当的模拟函数来测试函数 isgpiopinexported 中的逻辑并获得所需的结果。最后主程序如下所示:
func main() {
rasberry3Plus := gpio.Rasberry3Plus{}
gpio.IsGpioPinExported(rasberry3Plus, 23)
}
好了,本文到此结束,带大家了解了《在单元测试中模拟/伪造/替换硬件相关功能》,希望本文对你有所帮助!关注米云公众号,给大家分享更多Golang知识!
