我正在尝试模拟一个firestore本地客户端,以在我的Go项目中运行我的单元集成测试,但我已经打包了该项目,以便firestore在一个单独的包中初始化,并在其他包中使用。
所以我有点困惑于我应该在哪里定义本地firestore客户端和TestMain(m *testing.M)函数。下面是我的文件结构的基本概念。
main.go
main_test.go (This could also need firestore local connection)
pkg
|____datastore (where firestore clients are defined)
|___datastore.go
|___testing.go (I intended to have my TestMain(m *testing.M) function to run the emulator)
|___ .....
|____pkg2
|___myfile.go (go file that I use the firestore client to deal with firestore db)
|___myfile_test.go ( tests I am going to write that I need to use the local emulator)
|___ .....因此,我想知道如何才能实现这种测试。在等待帮助。我还得到了使用这个link的firestore模拟器的想法
发布于 2021-08-10 21:38:27
我也遇到过同样的问题。因为go没有在所有测试之前运行的测试函数,所以没有在运行之前设置仿真器的好方法。
根据我所看到的,有两个选择:(我希望听到更多的建议)。
emulators:exec scriptpath命令:https://firebase.google.com/docs/emulator-suite/install_and_configure。这将运行模拟器,然后运行脚本,在本例中为go测试。TestMain函数来设置firebase环境。这将使您能够单独运行每个测试,但在运行整个测试套件时会失败。最后一个问题的解决方案是删除go套件的并行处理,如下所示:go test ./... -v -race -p 1我采用了第二种解决方案,但您必须确保仿真器尚未运行,并在每次包测试后终止它。
所以就像这样:
shutdownCmd := exec.Command("bash", "-c", fmt.Sprintf("lsof -i tcp:%d | grep LISTEN | awk '{print $2}' | xargs kill -9", firestorePort))然后,您需要运行模拟器:
cmd := exec.Command("firebase", "emulators:start", "--only", "firestore")您必须监听仿真器的标准输出,并对其进行解析以检查何时:
ready
然后,在模拟器启动后,您可以运行
result = m.Run()它将运行当前包的测试套件。
最后,在对包进行测试之后,您必须向仿真器发送一个关闭信号(SIGINT):
syscall.Kill(cmd.Process.Pid, syscall.SIGINT)我考虑发布开源的解决方案,但没有足够的时间让它变得更好。如果您希望我与您分享完整的解决方案,请随时联系
https://stackoverflow.com/questions/66066212
复制相似问题