Scala : print only during unit test - scala

I want to display in the output window, but only during Unit tests and not during normal execution. I am using Scalatest for UT. Any idea how this can be handled in a clean way?
println("DataFrame results:")
dataFrame.show(100, false)

Related

How to execute scala tests programmatically

I'm looking for a way to execute scala tests (implemented in munit, but it could be also ScalaTest) programmatically. I want to perform more or less what sbt test does out-of-the box inside my own scala code, without running sbt (focusing on test discovery and execution and getting back a report).
I some something like this in mind:
object Test extends App {
val tests = TestDiscovery.discover("package.that.has.tests")
val reports = tests.foreach(test => test.execute())
// do something with the reports, maybe print to console
}
Is there any documentation related to this?
Scala Test has execute() and run().
In order to understand the impact of all the args it's worth looking at the Scala Test shell as well

Scala Parallel Print Hanging the console

I am new to this Scala world and I am trying some exercises from a book. So, I have an example that print a vector in sequential and parallel fashion. The former works perfectly and the later hangs the console.
Code
val v = Vector.range(0, 10)
v.foreach(println)
Code output
0123456789
But if I use the same code, but instead of using foearch, use par, it freezes the console
val v = Vector.range(0,10)
v.par.foreach(println)
The book I am using says that the output should be something like:
5678901234
But it hangs and the program never finishes.
Can someone explain me why?
It is better to post whole the program wich is hanged.
I've just tested it with scala 2.12.8 and jvm 1.8.0_161:
object MainClassss {
def main(args: Array[String]): Unit = {
val v = Vector.range(0,10)
(0 to 999999).foreach(_ => v.par.foreach(println))
}
}
the program has executed well with output and hasn't hanged.
If your program reproduced the aforementioned issue you need to take a thread dump by:
$ jstack <PID>
where PID is the process id.
Or you can take by jvisualvm jvm tool.
You can analyze why your program is hanged owing to caught thread dump if the program has some code blocking it from execution.

How to detect if a method is called by unit test?

How do I detect if a method is called by unit test in ScalaTest?
Edit: sorry, I was expressing wrongly the thing I wanted. I have a code block in a method which takes very long to finish (I cannot mock it) and it does not affect any logic. I want to skip that code block in the unit test. So I want to know whether it is called by unit test or normal running. If it is called by unit test, I skip it, otherwise, I let it runs normally.
I have a simple workaround by adding a trait like this:
trait AppConfig {
val isDebug:Boolean
}
use it in the places where I needs to check whether it is in debug mode:
class MyLogicClass {
_: AppConfig =>
def myMethod()={
if(isDebug){...}
}
}
Use a code coverage library such as scoverage for instance. It will generate reports that indicate you which parts of your code are used by unit tests.

Logging the time for each test (and/or testSuite) with scala test

I'm using scala test for my tests, and some time ago the builds started to fail sometimes because some (new, probably) test takes more than the limit allowed by the CI.
At first sight no test should take more than the timeout (10 minutes btw), and by the CI output it not easy to see which test is taking so much time (or how much time every test takes)
I can put simple println-based statements to print the current hour at the beginning/end of each test, but I was wondering if scala test provides some builtin feature for that?
If no, does any other scala test framework/library does?
EDIT: tried to wrap the test
I prefere some native method from the lib itself. I was trying to wrap a specs2 test in a method that would get the description and log the times, but I'm having difficulties in running the test in the moment I want to. I did something Like this:
class SomeSpec {
private def withTimeLog(x: Fragment) = {
println(s"Starting test: ${x.description}(${DateTime.now()})")
//what now?
x.executionResult
println(s"End of test: ${x.description}(${DateTime.now()})")
}
withTimeLog("Feature" should {
"stuff" in {
println(s"test: ${DateTime.now()}")
None.isEmpty must beTrue
}
})
}
and the result:
Starting test: End(2016-07-28T18:11:53.101+01:00)
End of test: End(2016-07-28T18:11:53.191+01:00)
[info] FeatureSpecV2
[info]
test running at 2016-07-28T18:11:54.037+01:00
[info] A test should
[info] + stuff
meaning, test was executed in the end, and not between the 2 print statements. Maybe Fragment.execution result doesn't run the test. Tried with several others. Any idea which method should I be using?
You can display execution times in specs2 by passing the showtimes option on the command line
sbt>testOnly *MySpec -- showtimes
Also if you use future matchers you can adjust a "time factor" with the timeFactor command line option.

How to disable Scala assertions in Eclipse?

I want to work with assertions in a Scala program. The assertions should be turned off for the final version of the program for increasing the performance.
There seem to be two Scala compiler flags enabling this (the first one requires an additional priority):
-Xelide-below
-Xdisable-assertions
However, activating those in the Scala Compiler Properties of my Scala project in Eclipse has no effect: the assertions are still executed.
The small program I used for testing:
object Test {
def main(args: Array[String]): Unit = {
assert(false)
}
}
I made sure the code is recompiled; I tested different (very high) priorities for -Xelide-below.
I use Eclipse Juno (4.2.0) and the Scala Plugi-In 2.1.0.nightly-2_09-201209040315-cc63a95 together with Scala 2.9.1-1 on a Windows 7 machine.
To disable assertions you should use Scala Compiler > Advanced > Xdisable-assertions.
Another way to disable them is to pass the option -Xdisable-assertions to Scala Compiler > Additional command line parameters