繁体   English   中英

如何在 sbt 运行的测试中获取构建 output 目录?

[英]How can I get the build output directory inside tests run by sbt?

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

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

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

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

在 ScalaTest 中,尝试传递target

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

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"
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM