scalatest

How to populate java.util.HashMap on the fly from Scala code?

断了今生、忘了曾经 提交于 2019-12-03 16:48:27
问题 I am unit testing java code from ScalaTest and would like to populate a java.util.HashMap within the same statement it gets declared. Is it possible to do this in Scala? 回答1: There are a bunch of different ways to accomplish this, only some of which have appeared in the answers thus far. Method One: Since java.util.HashMap has the constructor HashMap(Map<? extends K,? extends V> m) , you could pass it a valid Java Map. And you can do this trivially with Scala's helpful JavaConversions : scala

ScalaTest - writing custom matchers

时间秒杀一切 提交于 2019-12-03 13:45:56
I am running into a problem while writing a custom matcher for NodeSeq: private def matchXML(expected: NodeSeq) = new Matcher[NodeSeq] { def apply(left: NodeSeq): MatchResult = MatchResult(left xml_== expected, "XML structure was not the same (watch spaces in tag texts)", "XML messages were equal") } This compiles, but the following piece of code: val expected : NodeSeq = ... val xml : NodeSeq = ... xml should matchXML(expected) causes: error: overloaded method value should with alternatives: (beWord: XMLStripDecoratorTests.this.BeWord)XMLStripDecoratorTests.this.ResultOfBeWordForAnyRef[scala

Access ScalaTest test name from inside test?

依然范特西╮ 提交于 2019-12-03 12:30:37
Is it possible to access the name of the currently executing test, from within a ScalaTest test? (And how would I do it?) Background: I'm testing that my Data Access Object eventually throws an OverQuotaException if a user e.g. creates too many pages. These tests take rather long to run. To feel happier, I'd like to print the progress to stdout — and since there are quite many tests, I'd like to include the test name in the output, so I know what test is currently being run. (I didn't find any seemingly relevant function here: http://www.artima.com/docs-scalatest-2.0.M5/#org.scalatest.FreeSpec

How to measure and display the running time of a single test?

房东的猫 提交于 2019-12-03 09:40:52
问题 I have a potentially long-running test written with scalatest : test("a long running test") { failAfter(Span(60, Seconds)) { // ... } } Even if the test finishes within the timeout limit, its running time can be valuable to the person who runs the test. How can I measure and display the running time of this single particular test in scalatest's output? Update: Currently I measure the running time with my own function, just as in r.v's answer. I'd like to know if scalatest offers this

How to run all scalatest of a multi-modules sbt with intellij?

安稳与你 提交于 2019-12-03 06:53:57
I can run: a specific scala test either by right clicking on the test and choose run or if I have run it once previously, by selecting the test in run menu > run... I can run all junit tests by making a new run/debug configuration > + > junit > test kind = all in package > search for tests = in whole project I can run all scalatest (or junit) of one sub-project: right click on the project > run > scalatest in [module name] But if I do the same as (2) with a scala test (+ > scala test > test kind...) the first test fails very strangely (it seems an object is not instantiated) while the same

How to populate java.util.HashMap on the fly from Scala code?

流过昼夜 提交于 2019-12-03 05:48:53
I am unit testing java code from ScalaTest and would like to populate a java.util.HashMap within the same statement it gets declared. Is it possible to do this in Scala? There are a bunch of different ways to accomplish this, only some of which have appeared in the answers thus far. Method One: Since java.util.HashMap has the constructor HashMap(Map<? extends K,? extends V> m) , you could pass it a valid Java Map. And you can do this trivially with Scala's helpful JavaConversions : scala> import scala.collection.JavaConversions._ import scala.collection.JavaConversions._ scala> val myMap = Map

How to configure sbt test / ScalaTest to only show failures?

喜你入骨 提交于 2019-12-03 01:31:06
Is there a way to truncate the test results to only show result text for unit tests only when the unit test has failed? I'm working on a Scala project that has 850 unit tests, and the green text from the successful unit tests makes it difficult to focus on just the failures. Example of what I'm talking about: [info] - should have colors [info] - should not be dead //.... x 100 [info] - animals should not be rainbows *** FAILED *** [info] -"[rainbow]s" was not equal to "[ponie]s" (HappinessSpec.scala:31) What I would like is something that just shows the failure(s): [info] - animals should not

How to measure and display the running time of a single test?

你说的曾经没有我的故事 提交于 2019-12-03 00:03:50
I have a potentially long-running test written with scalatest : test("a long running test") { failAfter(Span(60, Seconds)) { // ... } } Even if the test finishes within the timeout limit, its running time can be valuable to the person who runs the test. How can I measure and display the running time of this single particular test in scalatest's output? Update: Currently I measure the running time with my own function, just as in r.v's answer. I'd like to know if scalatest offers this functionality already. the -oD option will give the duration of the test. For example, I use the following in

How to disable “Slow” tagged Scalatests by default, allow execution with option?

為{幸葍}努か 提交于 2019-12-02 22:16:40
I want to disable certain automated tests tagged as "Slow" by default but allow the user to enable their execution with a simple command line. I imagine this is a very common use case. Given this test suite: import org.scalatest.FunSuite import org.scalatest.tagobjects.Slow class DemoTestSuite extends FunSuite { test("demo test tagged as slow", Slow) { assert(1 + 1 === 2) } test("demo untagged test") { assert(1 + 1 === 2) } } By default, sbt test will run both tagged and untagged tests. If I add the following to my build.sbt: testOptions in Test += Tests.Argument("-l", "org.scalatest.tags.Slow

How can a private class method be tested in Scala?

心不动则不痛 提交于 2019-12-02 18:50:16
I have a companion object with a private method, like so: package com.example.people class Person(val age: Int) object Person { private def transform(p: Person): Person = new Person(p.age + 1) } I would like to test this method, with something like: class PersonSpec extends FlatSpec { "A Person" should "transform correctly" in { val p1 = new Person(1) val p2 = Person.transform(p1) // doesn't compile, because transform is private! assert( p2 === new Person(2) ) } } Any help on having test code access private methods? Actually, as it is written, I might be able to create a subclass of Person ,