【问题标题】:How can I get the build output directory inside tests run by sbt?如何在 sbt 运行的测试中获取构建输出目录?
【发布时间】:2020-07-27 09:02:24
【问题描述】:

我需要为一些测试创建目录和文件。我的项目使用 sbt 作为构建工具,通常的做法是使用 File.createTempFile 或类似的 API,但我讨厌这种做法。我希望我的测试创建的所有文件都驻留在输出目录 (<module>/target/) 内的某个位置,以便在我运行 clean 时将它们删除,但如果我需要它们来找出测试失败,则保留它们。

测试框架无关紧要:如果您的解决方案需要一个特定的框架,我会很乐意采用它或弄清楚它是如何做到的并使用它。

简而言之,我需要以下两个问题之一的答案:

  • 如何通过 sbt 运行的测试在构建输出目录中创建文件?
  • 如何从 sbt 运行的测试中找出当前项目的构建输出目录是什么?

【问题讨论】:

  • 你考虑过github.com/sbt/sbt-buildinfo 吗?
  • @SethTisue 我没有,这是一个有趣的想法。每个模块都需要一个不同的密钥,但我认为这是可行的吗?除了复制和粘贴之外,我对 BuildInfo 的操作不多。继续提交答案! :)
  • 我太懒了:-)

标签: scala sbt


【解决方案1】:

在 ScalaTest 中,尝试传递 target

settingKey[File]("Main directory for files generated by the build.")

config map-Dkey=value。例如,在build.sbt 中指定

Test / testOptions += Tests.Argument(s"-DtargetDir=${target.value}")

然后像这样定义测试

import org.scalatest._

class ExampleSpec extends fixture.FlatSpec with fixture.ConfigMapFixture with Matchers {
  "The config map" should "contain target directory used by sbt" in { configMap =>
    configMap should contain key "targetDir"
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 2016-02-09
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多