Cannot run tests with org.scalatest.tools.Runner from command line - scala

I have my test compiled in a directory: samplesuite
It works when running one Suite using org.scalatest.run.
Nothing happens when I try to run the directory that contains several suites.
scala -cp "..\mytestframework\target\scala-2.12\mytestframework-assembly-0.1.jar;../../Downloads/scalactic_2.12-3.0.5.jar;..\..\Downloads\scalatest_2.12-3.0.5.jar" org.scalatest.run samplesuite.SomeSpec
But it won't run when trying:
scala -cp "..\mytestframework\target\scala-2.12\mytestframework-assembly-0.1.jar;../../Downloads/scalactic_2.12-3.0.5.jar;..\..\Downloads\scalatest_2.12-3.0.5.jar" org.scalatest.tools.Runner -R samplesuite

Assuming tests are defined in package samplesuite, for example
package samplesuite
class SomeSpec extends FlatSpec with Matchers
and current working directory . is one level above samplesuite, then discover and run all suites with
scala -cp "..\mytestframework\target\scala-2.12\mytestframework-assembly-0.1.jar;../../Downloads/scalactic_2.12-3.0.5.jar;..\..\Downloads\scalatest_2.12-3.0.5.jar" org.scalatest.tools.Runner -R .
In general, when specifying runpath from which tests will be discovered with -R point to the root directory where your packages start, or to the .jar file generated with sbt test:package:
scala -cp target/scala-2.12/classes:scalatest_2.12-3.0.5.jar:scalactic_2.12-3.0.5.jar org.scalatest.tools.Runner -R target/scala-2.12/test-classes
scala -cp target/scala-2.12/classes:scalatest_2.12-3.0.5.jar:scalactic_2.12-3.0.5.jar org.scalatest.tools.Runner -R target/scala-2.12/how-can-i-perform-an-or-with-scalatest_2.12-0.1.0-SNAPSHOT-tests.jar

Related

scalac- create jar file from a scala file and its dependency

Using scalac only and not sbt can I create a runnable jar file from a scala file "script.scala" and a dependent library "x.jar"?
This article https://alvinalexander.com/scala/how-create-executable-jar-file-scalac-run-scala/ says to create the jar by:-
scalac script.scala -d script.jar
How do I include my dependent library x.jar here?
When I do the below I do not see x.jar when I extract script.jar
scalac -classpath "x.jar" script.scala -d script.jar

Running scala test from command line without SBT

I am trying to run the scala test from the command line without SBT and I am failing. I followed the documentation line-by-line.
import collection.mutable.Stack
import org.scalatest._
import flatspec._
import matchers._
class FirstSpec extends AnyFlatSpec with should.Matchers {
"A Stack" should "pop values in last-in-first-out order" in {
val stack = new Stack[Int]
stack.push(1)
stack.push(2)
stack.pop() should be (2)
stack.pop() should be (1)
}
}
Error message:
> scala -cp scalatest_2.13-3.2.5.jar org.scalatest.tools.Runner -R . -o -s FirstSpec.scala
No such file or class on classpath: org.scalatest.tools.Runner
Repository
ScalaTest has been modularised since 2.3.0 so just scalatest.jar artifact is not sufficient from raw shell. Build tools such as sbt would usually resolve all the transitive dependencies automatically, however if you are not using a build tool, then it is necessary to do that manually.
So download all the transitive dependencies and run something like
scala -cp scalatest_2.13-3.2.4.jar:scalatest-compatible-3.2.4.jar:scalatest-core_2.13-3.2.4.jar:scalactic_2.13-3.2.4.jar:scalatest-diagrams_2.13-3.2.4.jar:scalatest-matchers-core_2.13-3.2.4.jar:scalatest-shouldmatchers_2.13-3.2.4.jar:scalatest-flatspec_2.13-3.2.4.jar:scala-xml_2.13-1.3.0.jar org.scalatest.run ExampleSpec
or given all the transitive jars are in the same directory
scala -cp '*' org.scalatest.run ExampleSpec
or coursier can help you fetch and build the correct classpath
scala -cp "$(cs fetch --classpath org.scalatest:scalatest_2.13:3.2.4)" org.scalatest.run ExampleSpec
or use coursier to launch the main class from directory containing compiled tests
cs launch org.scalatest:scalatest_2.13:3.2.4 -M org.scalatest.run
or launch the default main runner which provides basic GUI by providing the run path -R
cs launch org.scalatest:scalatest_2.13:3.2.4 -- -R .
For record here are all the transitive dependencies of scalatest.jar
cs resolve org.scalatest:scalatest_2.13:3.2.5
org.scala-lang:scala-library:2.13.4:default
org.scala-lang:scala-reflect:2.13.4:default
org.scala-lang.modules:scala-xml_2.13:1.2.0:default
org.scalactic:scalactic_2.13:3.2.5:default
org.scalatest:scalatest-compatible:3.2.5:default
org.scalatest:scalatest-core_2.13:3.2.5:default
org.scalatest:scalatest-diagrams_2.13:3.2.5:default
org.scalatest:scalatest-featurespec_2.13:3.2.5:default
org.scalatest:scalatest-flatspec_2.13:3.2.5:default
org.scalatest:scalatest-freespec_2.13:3.2.5:default
org.scalatest:scalatest-funspec_2.13:3.2.5:default
org.scalatest:scalatest-funsuite_2.13:3.2.5:default
org.scalatest:scalatest-matchers-core_2.13:3.2.5:default
org.scalatest:scalatest-mustmatchers_2.13:3.2.5:default
org.scalatest:scalatest-propspec_2.13:3.2.5:default
org.scalatest:scalatest-refspec_2.13:3.2.5:default
org.scalatest:scalatest-shouldmatchers_2.13:3.2.5:default
org.scalatest:scalatest-wordspec_2.13:3.2.5:default
org.scalatest:scalatest_2.13:3.2.5:default

How do you run a Scala bash script with TypeSafe Activator?

How do you run a Scala shellscript with Activator?
Before using Activator you would use
#!/bin/sh
exec scala "$0" "$#"
but with Activator
#!/bin/sh
exec activator "$0" "$#"
does not work. I would prefer not having both Activator and a separate Scala install to avoid duplicate repositories if possible. If it is not possible knowing that would be helpful.
http://www.scala-lang.org/documentation/getting-started.html#script_it
We don't have the scala executable rolled into the activator download yet, so to do this you have to download Scala separately (http://www.scala-lang.org/download/ pick the "scala binaries" instead of activator). We would like activator to come with a bin/ directory that has the scala executable in it, but it currently doesn't.

Running scalatest classes from sbt file

I am running my test class from sbt file using task
scalaTaskRun := {
val test = (runMain in Compile).fullInput(" org.scalatest.tools.Runner -s package.tests.TestClass1 -h ReportOutput").evaluated
}
to get a html report output for single test class. But I don't want to add -s TestClass2 again for running one more tests and so on...
If I want to run many test classes from sbt file just like running testNG suite xml which contains more than one test classes. How can the same thing be achieved in sbt scalatest??
I tried running with the runpath command...
Runner -R target\\folder\\classes -w package.testcases -h reportFolder
But it's not running the compiled test classes from classes folder.
Kindly help in fixing it.
You can tell sbt which tests you want to run:
testOnly package.testcases.* -- -h reportFolder
everything after -- gets passed through to the test framework (e.g. ScalaTest).

scala: Error occurred during initialization of VM on Ubuntu 12.04

I tried to work with easy example of scala language.
I installed sbt successfully. When run sbt -h I see help message from sbt:
Usage: sbt [options]
-h | -help print this message
-v | -verbose this runner is chattier
-d | -debug set sbt log level to debug
-no-colors disable ANSI color codes
-sbt-create start sbt even if current directory contains no sbt project
-sbt-dir <path> path to global settings/plugins directory (default: ~/.sbt)
-sbt-boot <path> path to shared boot directory (default: ~/.sbt/boot in 0.11 series)
-ivy <path> path to local Ivy repository (default: ~/.ivy2)
-mem <integer> set memory options (default: 1536, which is -Xms1536m -Xmx1536m -XX:MaxPermSize=384m -XX:ReservedCodeCacheSize=192m) ....
but when I tried to run in the directory with scala assignments (it's Coursera course) I caught error:
nazar_art#nazar-desctop:~/scala/example$ sbt
Detected sbt version 0.12.1
Starting sbt: invoke with -help for other options
Error occurred during initialization of VM
Could not reserve enough space for object heap
but it should open the sbt command prompt. Smt like this:
shell$ cd /path/to/progfun-project-directory # This is the shell of the operating system
shell$ sbt
> _ # This is the sbt shell
UPDATE:
After I add this following line
java -Xms64M -Xmx256M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=64M -jardirname $0/sbt-launch.jar "$#"
at /usr/bin I see next output:
nazar_art#nazar-desctop:/usr/bin$ sbt
bash: /usr/bin/sbt: Permission denied
nazar_art#nazar-desctop:/usr/bin$ sudo sbt
sudo: sbt: command not found
- Why this happen?
- How to solve this issue?
First of all, The example folder name (and the folder name that you have installed sbt) cannot include "!". this probably applies to other special characters as well. Even, I recommend to install SBT in a folder that has no space on its path (and the same thing for examples folder).
For example, if you have installed SBT (or examples folder) on a path like:
/Users/dashti/My Programs/sbt-0.12.1
You can re-install it a path like:
/Users/dashti/MyPrograms/sbt-0.12.1
By applying this naming change, if the error is gone, please ignore the rest of this post.
Otherwise, to fix the mentioned error, first find sbt executable script, then edit it and correct any problem in it:
If you are using a UNIX-based operating system, please follow these steps:
1- Find SBT: Open a terminal and type this command:
which sbt
and you will receive a path like:
/Users/dashti/Documents/scala-2.10.0/bin/sbt
and in this file, there is a command for executing a java program, like:
java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch.jar "$#"
2- Edit sbt script: by entering this command in terminal:
vim /Users/dashti/Documents/scala-2.10.0/bin/sbt
you will be able to edit this file and maybe you should change the memory usage arguments to a lower specification ( You should think about whether you have enough RAM to be running the sbt). For example, this one will be a better choice:
java -Xms64M -Xmx256M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=64M -jar `dirname $0`/sbt-launch.jar "$#"
SBT needs more memory allocated to the JVM.
This can be done in build.sbt:
javaOptions ++= Seq(
"-Xms64m",
"-Xmx256M",
"-XX:+CMSClassUnloadingEnabled",
"-XX:MaxPermSize=64M"
);
Creating an sbt file(no extentions) and running ./sbt is better, but with the code below(#MohammadDashti's suggestion), sbt-launch.jar needs to be in the project folder.
java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled \
-XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch.jar "$#"
Later you could add javaagent:/path/to/jrebel -Drebel.remoting_plugin=true and re-deploy changes on save with a free JRebel for Scala license.