scalatest

can you test nested functions in scala?

陌路散爱 提交于 2019-12-05 18:16:01
问题 Is there any way to test a nested function (ideally with ScalaTest)? For example, is there a way to test g() in the below code: def f() = { def g() = "a string!" g() + "– says g" } 回答1: g is not visible outside of f , so I daresay no, at least not without reflection. I think testing g would break the concept of unit testing, anyway, because you should never test implementation details but only public API behaviour. Tracking an error to a mistake in g is part of the debugging process if tests

How to correctly use Spark in ScalaTest tests?

这一生的挚爱 提交于 2019-12-05 16:09:48
I have multiple ScalaTest classes which use BeforeAndAfterAll to construct a SparkContext and stop it afterwards like so: class MyTest extends FlatSpec with Matchers with BeforeAndAfterAll { private var sc: SparkContext = null override protected def beforeAll(): Unit = { sc = ... // Create SparkContext } override protected def afterAll(): Unit = { sc.stop() } // my tests follow } These tests run fine when started from IntelliJ IDEA, but when running sbt test , I get WARN SparkContext: Another SparkContext is being constructed (or threw an exception in its constructor). This may indicate an

Setting up actions for multiple test folders in SBT

只谈情不闲聊 提交于 2019-12-05 03:09:04
In relation to a previous question , I'd like to have multiple test folders for different types of test and be able to execute the tests contained in each folder with a separate SBT action. For example, an action 'test-unit' would run only the tests contained under the folder src/test/scala/unit , and a 'test-functional' action would run only the tests under src/test/scala/functional . How would we write actions to do this? If you are using xsbt 0.10.0, you can easily create additional test configurations by defining the a full build configuration in a Scala file located in your project folder

Scalacheck won't properly report the failing case

六眼飞鱼酱① 提交于 2019-12-05 02:01:14
I've wrote the following spec "An IP4 address" should "belong to just one class" in { val addrs = for { a <- Gen.choose(0, 255) b <- Gen.choose(0, 255) c <- Gen.choose(0, 255) d <- Gen.choose(0, 255) } yield s"$a.$b.$c.$d" forAll (addrs) { ip4s => var c: Int = 0 if (IP4_ClassA.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassB.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassC.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassD.unapply(ip4s).isDefined) c = c + 1 if (IP4_ClassE.unapply(ip4s).isDefined) c = c + 1 c should be (1) } } That is very clear in its scope. The test passes successfully but

How make tests always run in same order in Scalatest?

此生再无相见时 提交于 2019-12-04 23:13:03
We use Spec trait for our tests in ScalaTest. when we run the entire suite, it does not always run in the same order. Most answers in google suggest defining a Suite and specifying all the test names. But this requires us to add the test name every time we add a new test. Is it possible to use the DiscoverySuite itself and define the test execution order? Like run the tests in alphabetical order. I looked at extending the DiscoverySuite but DiscoverySuite seems to be private to scalatest. ---More info---- By ordering i mean, If there are tests A, B, C. class A extends Spec {..} class B extends

ScalaTest in Eclipse: Running it gives NullPointer Exception

主宰稳场 提交于 2019-12-04 20:13:29
EDIT: This issue was a bug with sbteclipse and has been fixed When I right click on a ScalaTest suite and select Run as ScalaTest File, I get a NullPointer Exception. As a plugin, I have ScalaTest for Scala IDE 2.9.3.v-3-2_09-201309130843-55f5c32 As a jar in my library, I have scalatest_2.9.0-2.0.M5b.jar(tried scalatest_2.9.3-RC2-2.0.M5b.jar as well) Tests run in SBT Side Question: If it is a Scalatest compatibility issue between the jar and the plugin, how do I get the appropriate jar which matches the plugin? I looked here https://oss.sonatype.org/content/groups/public/org/scalatest/ but I

ParallelTestExecution with FlatSpec, Selenium DSL and Spring

折月煮酒 提交于 2019-12-04 18:43:35
I am using Scalatest, FlatSpec, Spring, Selenium DSL and BeforeAndAfterAll. One of these things seems to stop ParallelTestExecution working properly. This is what happens when I run a class with two tests: One Browser opens and does some beforeAll stuff (but not Spring stuff) Another browser opens and does the beforeAll stuff Second browser gets used for first test then closes Another browser opens and does beforeAll stuff followed by second test First and Third browsers close So basically the test runs exactly the same as without ParallelTestExecution except that an extra window has opened? I

Run just a specific scalatest test from sbt

不羁的心 提交于 2019-12-04 17:10:12
问题 sbt 's test-only command can be used to run the tests found in a specific test class. With JUnit tests you can use test-only to run specific methods on a test class e.g. test-only mypackage.MyTestClass.test1Equals1 to run just that method. Is such a thing possible with scalatest's more free-form test syntax, presumably by working out the name it uses internally to reference a specific test? If it isn't possible in FreeSpec (which is easy to imagine given its nature) is there a way to do it

Using a HavePropertyMatcher for collection elements in ScalaTest?

北城余情 提交于 2019-12-04 08:36:12
I've been using ScalaTest's FeatureSpec for a couple of days now and I'm trying to understand if it's possible to define the following spec using the built-in matchers (and if not, how I can write a suitable custom matcher). Suppose I have the class Book: case class Book(val title: String, val author: String) and in my test I have a List of books: val books = List(Book("Moby Dick", "Melville")) Now, I would like to specify that the books list should contain a book with the title "Moby Dick". I would like to write something like: books should contain (value with title "Moby Dick") I can't seem

Mocking case classes with primitive types

一个人想着一个人 提交于 2019-12-04 08:21:21
Consider following types structure: trait HasId[T] { def id: T } case class Entity(id: Long) extends HasId[Long] Let's say, we want to mock Entity class in some test. val entityMock = mock[Entity] Mockito.when(entityMock.id).thenReturn(0) Playing such test results in throwing NullPointerException (in second line), probably because of scala compiler behaviour with wrapping primitive types (if we replace Long with String, test executes correctly). An exception or error caused a run to abort. java.lang.NullPointerException at com.test.Entity$MockitoMock$1085095743.id(Unknown Source) at com.test