如何使用相对目录测试Go程序?

huangapple go评论94阅读模式
英文:

How to test Go programs with relative directories?

问题

根据测试文档:

“该软件包是在临时目录中构建的,因此不会干扰非测试安装。”

因此,与可执行文件相关的目录的代码将不存在于Temp\go-build......相对路径中。

换句话说,给定以下内容:

.\helloplanet.go

.\planets\planetary.res

.\helloplanet_test.go

go test

在Temp\go-build...中生成一个可执行文件,但它不会在那里重新创建一个planets\子目录,因此helloplanet.exe中寻找planets\planetary.res的任何代码当然找不到它。

应该如何处理这个问题?

英文:

From the testing docs:

"The package is built in a temporary directory so it does not interfere with the non-test installation."

So any code working with directories relative to the executable will not be present relative to Temp\go-build......

In other words, given the following:

.\helloplanet.go

.\planets\planetary.res

.\helloplanet_test.go

go test

Produces an exe in Temp\go-build... but it doesn't also re-create a planets\ subdir there, so any code in helloplanet.exe looking for planets\planetary.res of course won't find it.

How should one deal with this?

答案1

得分: 8

这里有一个实际有效的解决方案。我在jetbrains论坛中找到了这个解决方案。以下是用户Daniil Maslov提出的答案:


这个技巧实际上非常简单,就是获取当前执行的路径,并在项目根目录后面添加..

创建一个新的目录和文件,比如testing_init.go,内容如下:

package testing_init

import (
  "os"
  "path"
  "runtime"
)

func init() {
  _, filename, _, _ := runtime.Caller(0)
  dir := path.Join(path.Dir(filename), "..")
  err := os.Chdir(dir)
  if err != nil {
    panic(err)
  }
}

之后,只需将该包导入到任何测试文件中:


package main_test

import (
  _ "project/testing_init"
)

现在你可以从项目根目录指定路径了。

英文:

Here's a solution that actually works. I found this buried in the jetbrains forum. Here's the answer that user Daniil Maslov came up with :


The trick is actually very simple and is to get the current executing and add .. to the project root.

Create a new directory and file like testing_init.go with the following content:

package testing_init

import (
  "os"
  "path"
  "runtime"
)

func init() {
  _, filename, _, _ := runtime.Caller(0)
  dir := path.Join(path.Dir(filename), "..")
  err := os.Chdir(dir)
  if err != nil {
    panic(err)
  }
}

After that, just import the package into any of the test files:


package main_test

import (
  _ "project/testing_init"
)

Now you can specify paths from the project root

答案2

得分: 3

尽管测试文件是在临时位置构建的,但你的工作目录将会是你期望的位置。如果你在测试函数内部执行os.Getwd(),你会发现你的工作目录是包目录。只要你的包所在位置在GOPATH中,无论是在包内部运行go test还是使用完整的导入路径调用它,这个工作方式都应该是一样的。

在我的包中,我经常有一个testdata目录,通过相对路径访问它,并且没有遇到任何问题。

英文:

Though the test files are built in a temporary location, your working directory will be where you expect it. If you execute an os.Getwd() from within a test function, you'll see that your working directory is the package directory. This should work the same when running go test from within your package, or calling it with the full import path, as long as your package's location is in GOPATH.

In my packages, I often have a testadata directory, which is accessed via it's relative path, and have had no issues.

huangapple
  • 本文由 发表于 2014年3月17日 23:05:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/22457861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定