scala 多项目中的 SBT 测试依赖:使测试代码可用于依赖项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8193904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:40:52  来源:igfitidea点击:

SBT Test-Dependencies in Multiprojects: Make the Test-Code Available to Dependent Projects

scalatestingsbt

提问by Gamlor

I've a SBT multi-project where some projects have dependencies to each other. Like this:

我有一个 SBT 多项目,其中一些项目相互依赖。像这样:

 lazy val coreProject: Project = Project(
    id = "core-project",
    base = file("./core-project"),
    // other stuff
    ))

  lazy val extensions: Project = Project(
    id = "extensions",
    base = file("./extensions"),
    dependencies = Seq(coreProject)
  )

Now I have some test-code in the 'core' project in the test-folder. There are also stuff like mocks and test-utilities. Now I would like to use those test utilities in the tests of the extensions. For production code this works, since I've declared a dependency. However it seems that dependency doesn't hold for the tests. When I run the tests I get compilation error for missing classes. Those classes are from the test-code in the core-project.

现在我在 test-folder 的“核心”项目中有一些测试代码。还有模拟和测试实用程序之类的东西。现在我想在扩展测试中使用这些测试实用程序。对于生产代码,这是有效的,因为我已经声明了一个依赖项。但是,似乎依赖项不适用于测试。当我运行测试时,我收到缺少类的编译错误。这些类来自核心项目中的测试代码。

How can I tell sbt that the dependency also should include the test-code for the test-scope? So that I can reuse my mocks in the test-code of the 'exension'-project?

我如何告诉 sbt 依赖项还应该包括测试范围的测试代码?这样我就可以在“扩展”项目的测试代码中重用我的模拟?

回答by retronym

Like so:

像这样:

dependencies = Seq(coreProject % "compile->compile;test->test")

This is discussed in the section "Per-configuration classpath dependencies" in then Getting-Started-Multi-Projectguide.

这在“入门多项目”指南中的“每个配置类路径依赖项”部分中进行了讨论。

回答by Ryan Gross

You can also do this with a .dependsOn(coreProject % "compile->compile;test->test")after the initial project declaration.

您也可以.dependsOn(coreProject % "compile->compile;test->test")在初始项目声明之后使用 a 来执行此操作。

lazy val coreProject = Project("core-project")
lazy val extensions = Project("extensions").dependsOn(coreProject % "compile->compile;test->test")