简体   繁体   中英

Can I run JUnit 4 to test Scala code from the command line?

If so, how? I haven't come across the proper incantation yet.

If not, what's the best approach to unit-testing Scala code from the command line? (I'm a troglodyte; I use IDEs when I have to, but I prefer to play around using Emacs and command-line tools.)

Since compiled Scala is just Java bytecode (OK, with a lot more $ characters in class names), it would be exactly as for running JUnit 4 tests against Java code, ie from the command line by passing the test classes as arguments to org.junit.runner.JUnitCore . As JUnit 4 out of the box only has command line support, you don't even have to worry about suppressing GUI based test runners.

That said, the specific Scala test frameworks (ScalaTest, ScalaCheck ) do provide a more idiomatic set approach to testing code written in this more functional language.

You may be interested in ScalaTest .

ScalaTest is a free, open-source testing tool for Scala and Java programmers. It is written in Scala, and enables you to write tests in Scala to test either Scala or Java code. It is released under the Apache 2.0 open source license. Because different developers take different approaches to creating software, no single approach to testing is a good fit for everyone. In light of this reality, ScalaTest is designed to facilitate different styles of testing.

See the Runner documentation for how to run tests from the command line.

The suggestion of ScalaTest -- or any of the other Scala-specific frameworks, for that matter, is very good. I'd like to point to something else, though.

SBT .

SBT is a build tool, like Ant, Maven or Make. One interesting aspect of it, which will matter to us, is that it is Scala-based. I don't mean it has special capabilities to handle Scala code or that it is written in Scala, though both these things are true. I mean it uses Scala code , instead of XML like Maven and Ant, as the configuration source.

That in itself is interesting. Just today I saw a wonderful example of separating test sources from program sources, which I post here just because its so cool.

// on this project we keep all sources, whether they be Scala or Java, and whether they be
// regular classes or test classes, in a single src tree.
override def mainScalaSourcePath = "src"
override def mainJavaSourcePath = "src"
override def testScalaSourcePath = "src"
override def testJavaSourcePath = "src"
override def mainResourcesPath = "resources"

// distinguish main sources from test sources
def testSourceFilter =
  "Test*.scala" | "Test*.java" |
  "AbstractTest*.scala" | "AbstractTest*.java" |
  "ScalaTestRunner.scala"
def mainSourceFilter = ("*.scala" | "*.java") - testSourceFilter
override def mainSources = descendents(mainSourceRoots, mainSourceFilter)
override def testSources = descendents(testSourceRoots, testSourceFilter)

But what makes it even more interesting is that SBT works like a console. You run "sbt", and you get dropped into a console-like interface, from which you can type commands like, for instance, "test", and have your tests run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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