How do you structure imports for testing inside a module of your project?

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

How do you structure imports for testing inside a module of your project?

问题

我正在尝试理解如何将一个Go项目结构化为存储在单独的源代码仓库(位于主机example.com上)中的子模块,但是当我这样做时,我不知道如何运行模块内的测试。在下面的示例中,我做错了什么?非常感谢任何帮助!

mkdir -p src/example.com/john/tool

echo "package tool"       >> src/example.com/john/tool/book.go
echo ""                   >> src/example.com/john/tool/book.go
echo "type Book struct {" >> src/example.com/john/tool/book.go
echo "  Title string"     >> src/example.com/john/tool/book.go
echo "}"                  >> src/example.com/john/tool/book.go
echo ""                   >> src/example.com/john/tool/book.go

echo "package tool"       >> src/example.com/john/tool/book_test.go
echo ""                   >> src/example.com/john/tool/book_test.go
echo "import ("           >> src/example.com/john/tool/book_test.go
echo "  \"tool\""         >> src/example.com/john/tool/book_test.go
echo "  \"testing\""      >> src/example.com/john/tool/book_test.go
echo ")"                  >> src/example.com/john/tool/book_test.go
echo ""                   >> src/example.com/john/tool/book_test.go
echo "func TestBook(t *testing.T) { }" >> src/example.com/john/tool/book_test.go
echo ""                   >> src/example.com/john/tool/book_test.go

export GOPATH=`pwd`

go test example.com/john/tool

当我运行这个测试时,我看到以下错误:

# example.com/john/tool
src/example.com/john/tool/book_test.go:4:3: cannot find package "tool" in any of:
	/usr/local/go/src/tool (from $GOROOT)
	/Users/john/app/src/tool (from $GOPATH)
FAIL	example.com/john/tool [setup failed]

显然,book_test.go无法导入"tool"包,你可能可以输入完整路径,但是当我在GitHub上查看时,没有人在他们的模块中这样做。所以我不明白我做错了什么。

英文:

I am attempting to understand how to structure a go project into sub modules stored in separate source code repositories (on host example.com), however when I do, I am not sure how to run the tests that are within a module. What am doing wrong in the following example, any help is much appreciated!!

<!-- language: lang-bash -->

mkdir -p src/example.com/john/tool

echo &quot;package tool&quot;       &gt;&gt; src/example.com/john/tool/book.go
echo &quot;&quot;                   &gt;&gt; src/example.com/john/tool/book.go
echo &quot;type Book struct {&quot; &gt;&gt; src/example.com/john/tool/book.go
echo &quot;  Title string&quot;     &gt;&gt; src/example.com/john/tool/book.go
echo &quot;}&quot;                  &gt;&gt; src/example.com/john/tool/book.go
echo &quot;&quot;                   &gt;&gt; src/example.com/john/tool/book.go

echo &quot;package tool&quot;       &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;&quot;                   &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;import (&quot;           &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;  \&quot;tool\&quot;&quot;         &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;  \&quot;testing\&quot;&quot;      &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;)&quot;                  &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;&quot;                   &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;func TestBook(t *testing.T) { }&quot; &gt;&gt; src/example.com/john/tool/book_test.go
echo &quot;&quot;                   &gt;&gt; src/example.com/john/tool/book_test.go

export GOPATH=`pwd`

go test example.com/john/tool

When I run this test, this is the error I am seeing:

# example.com/john/tool
src/example.com/john/tool/book_test.go:4:3: cannot find package &quot;tool&quot; in any of:
	/usr/local/go/src/tool (from $GOROOT)
	/Users/john/app/src/tool (from $GOPATH)
FAIL	example.com/john/tool [setup failed]

Obviously book_test.go can't import the "tool" package, an you could probably put in the full path, but when I look in github, no one does that in their modules. So I don't understand what I am doing wrong.

答案1

得分: 2

你的代码中的这一行

import "tool"

是有问题的。标准库中没有名为tool的包,而你的tool包的导入路径是example.com/john/tool

只需删除这个导入语句即可。没有必要导入当前包(而且这是不可能的,因为这将导致(退化的)导入循环)。

英文:

Your line

import &quot;tool&quot;

is the offender. There is no package tool in the standard library and your package tool has an import path of example.com/john/tool.

Just drop that import. There is no need to import the current package (and it is impossible as this would be a (degenerated) import cycle).

huangapple
  • 本文由 发表于 2016年8月18日 18:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/39015621.html
匿名

发表评论

匿名网友

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

确定