Daniel C. Sobral
Daniel C. Sobral

Reputation: 297275

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

I need to create directories and files for some tests. My project uses sbt as the build tool, and common practice is to use File.createTempFile or similar APIs, but I abhor that practice. I want all files created by my tests to reside somewhere inside the output directory (<module>/target/), so that they'll be removed when I run clean, but otherwise preserved if I have need of them to figure out test failures.

The test framework is not relevant: if your solution requires a particular framework, I'll happily adopt it or figure out how it does the trick and use that.

In short, I need the answer to one of these two questions:

Upvotes: 2

Views: 509

Answers (1)

Mario Galic
Mario Galic

Reputation: 48430

In ScalaTest, try passing target

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

to config map as -Dkey=value. For example, in build.sbt specify

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

and then define test like so

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

Upvotes: 1

Related Questions