I have been using ScalaTest for a while and I find pretty useful the ability to Tag your tests and run just those with a specific Tag from the command line.
Is there anything similar in Specs2?
I know that you can run a specific test class with testOnly but I would like to just run a test with a specific Tag within a Specification.
Here is how to do it:
import org.specs2.mutable._
class MySpec extends Specification {
tag("fast")
"example1" >> ok
tag("slow")
"example2" >> ok
}
Then in sbt
sbt> test-only *MySpec* -- include fast
You will find more information here.
I am trying to run some test in scala play framework. Here is the code that I have written
package com.sentrana.mmcore.integrationTests
import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import play.api.libs.json._
class ApplicationSpec extends Specification with Tags {
"Application" should {
"work" in new WithApplication {
route(FakeRequest(GET, "/boum")) must beNone
}
}
}
This is the command that I am using to run the test -
test-only com.sentrana.mmcore.integrationTests.ApplicationSpec
I am not getting any error. But it is showing me that 0 tests have been.
If you add you test using IntellijIDEA make sure that the file has .scala extension. The IDE does not show errors but the activator cannot find the test because it is searching for scala files.
I'm having some issues today with running a simple TestKit test in Intellij. The tests are for Scala code (I have the Scala plug-in for Intellij) and are based on Ray Roestenburg's example.
The Intellij project was created using a "Maven Module" which I then added all the dependencies to and created my project. The tests are located in the following place:
ActorBlast/src/test/scala/basicTest.scala
I'm basically "right-clicking" on the test and selecting "Run". What I get is the following error:
"C:\Program Files\Java\jdk1.7.0_25\bin\java" -Didea.launcher.port=7540...
Testing started at 2:29 PM ...
Unable to load a Suite class. This could be due to an error in your runpath.
Missing class: BasicActorSpec java.lang.ClassNotFoundException:
BasicActorSpec at
java.net.URLClassLoader$1.run(URLClassLoader.java:366) at
java.net.URLClassLoader$1.run(URLClassLoader.java:355) at
java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(URLClassLoader.java:354) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
org.scalatest.tools.Runner$$anonfun$35.apply(Runner.scala:2393) at
org.scalatest.tools.Runner$$anonfun$35.apply(Runner.scala:2391) at
scala.collection.TraversableLike$$anonfun$filter$1.apply(TraversableLike.scala:264)
at scala.collection.immutable.List.foreach(List.scala:318) at
scala.collection.TraversableLike$class.filter(TraversableLike.scala:263)
at scala.collection.AbstractTraversable.filter(Traversable.scala:105)
at
org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:2391)
at
org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1006)
at
org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1005)
at
org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:2659)
at
org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1005)
at org.scalatest.tools.Runner$.run(Runner.scala:845) at
org.scalatest.tools.Runner.run(Runner.scala) at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:144)
at
org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606) at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 0
I can't figure out what this means. I've done a lot of searching but can't seem to find an answer. Note that the class the runner is complaining about not finding is the class I'm trying to test/run. The basicTest.scala looks like this:
// Testing specific imports
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{ShouldMatchers, WordSpecLike, BeforeAndAfterAll}
import akka.testkit.{TestKit, DefaultTimeout, ImplicitSender}
// Actor specific imports
import akka.actor.{ActorRef, Actor, ActorSystem, Props}
// Misc. needed imports
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
// In order to run tests in this module you need to use JUnitRunner (as per scalatest.org)
#RunWith(classOf[JUnitRunner])
class BasicActorSpec extends TestKit(ActorSystem("BasicActorSpec", ConfigFactory.parseString(BasicActorSpec.config)))
with DefaultTimeout with ImplicitSender with WordSpecLike with ShouldMatchers with BeforeAndAfterAll {
import BasicActorSpec._
val echoRef = system.actorOf(Props[EchoActor])
val forwardRef = system.actorOf(Props[ForwardActor])
override def afterAll {
shutdown(system)
}
/**
* The actual tests...
*/
"An EchoActor" should {
"Respond with the same message it receives" in {
within(500 millis) {
echoRef ! "test"
expectMsg("test")
}
}
}
"A Forwarding Actor" should {
"Forward a message it receives" in {
within(500 millis) {
forwardRef ! "test"
expectMsg("test")
}
}
}
}
/**
* Companion object of test class
*/
object BasicActorSpec {
val config =
"""
|akka {
| loglevel = "Warning"
|}
""".stripMargin
/**
* Classes of Actors used in testing
*/
class EchoActor extends Actor {
def receive = {
case msg => sender ! msg
}
}
class ForwardActor(next: ActorRef) extends Actor {
def receive = {
case msg => next ! msg
}
}
}
Any help as to why I am getting this error would be GREATLY appreciated.
Run build the project - It helped me to resolve that issue that could have happened to me when I cleared Cache IDEA :) while trying to tackle another issue
This is how I solved same exception:
--> Right click on your project folder in IDE:
--> Click Add Framework Support
--> Then Check Scala
--> Click OK
My project already had the setup as mentioned by #Rustam Aliyev. Still was getting the same exception. Rebuilding the project did not help either. Quite weird ; but Restarting the IDE helped to solve the issue
This issue happened to me recently when I was trying to run tests in an inherited Scala project using IntelliJ IDEA 2018 (Community Edition). Below steps helped me to fix it:
File → Project Structure → {Choose specific module} → "Paths" tab → Select "Use module compile output path" → Modify "Test output path" to point to test-classes folder. For example:
Output path: /home/rustam/IdeaProjects/{project}/{module}/target/scala-2.12/classes
Test output path: /home/rustam/IdeaProjects/{project}/{module}/target/scala-2.12/test-classes
IntelliJ does not like multiple Scala classes defined in one file, so make sure the test class is named the same as the test file and nest other helper classes, which you can refactor later as necessary.
I met this issue when I used Gatling
I fix it by replacing gatling-classes to test-classes on File -> Project Structure -> Modules -> Module Name -> Paths -> Test output path
You need to set up the Scala SDK.
1.) Usually, intelliJ will ask you by showing a message on right hand corner of your editor
2.) You can do it by yourself as mentioned on the https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html
In my case, I was missing the Scala facet in my module.
https://blog.jetbrains.com/scala/2010/09/02/project-configuration-explained/
I got rid of the error once I configured module properly.
You can try to recompile the test classes:
sbt test:compile
If you are using IntelliJ to run scalatest make sure the class paths are correct. For example:
/dummyApp
your build.sbt should look like, name := "dummyApp". If you name it name := "dummy App" you will get errors.
In my case I had in Preferences -> Build, Execution, Deployment -> sbt
Use sbt shell for build and import checkbox enabled
And the test was not in the expected directory src/test but in src/it (integrated test).
I encountered the error when the test class was not part of any package.
I am using the multi maven module and tried all possibilities here but not able to fix this. But for me I closed the complete IntelliJ -> removed .idea folder -> deleted managed projects from recent projects window.-> reimport the project did the job.
This kind of thing keeps happening to me every now and then. All the more concrete suggestions above have merit. Another possibility (which worked this time for me): Edit the run configuration for your tests; click "Use sbt"; run the tests (this should have no problem since it's just using the sbt-shell to run the tests); now unselect "Use sbt" and try it. It worked for me.
Another one in the long list of checks, if you do not use unique test names you get this error without any hint, in a long test suite it is easier to miss.
Failed
test("Check thing 1") {
class TestClass1 extends MainClass1{
... }
test("Check thing 1") {
class TestClass2 extends MainClass2{
... }
Successful
test("Check thing 1") {
class TestClass1 extends MainClass1{
... }
test("Check thing 2") {
class TestClass2 extends MainClass2{
... }
I got the same message on Idea 2021.1. I tried all the ways above but what helped me is running Scala-tests via sbt like this:
sbt "; project nameOfProject; testOnly some.package.SomeTest"
After that I could debug the same tests via Idea.
I had two modules marked as Test Sources Root and thus it didn't like that. So I unmarked one and then it worked great
I'm using the Play Framework v2.04 with Scala on OS X (installed via Homebrew). Everything works as expected, however I can't seem to get the basic Hello World sample specs2 test from the Play website's documentation to run. This is the code within my /app/test/example.scala file:
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
class HelloWorldSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11)
}
"start with 'Hello'" in {
"Hello world" must startWith("Hello")
}
"end with 'world'" in {
"Hello world" must endWith("world")
}
}
}
However when I run play test, I get the following error:
[error] /app/test/example.scala:3: object test is not a member of package play.api
AFAIK, the test object should be a member of the play.api package (according to the API reference documentation).
Any ideas on how to resolve this?
Thanks!
Create test directory under the project root directory and move the example.scala from app/test to test.
In IntelliJ IDEA "Mark Directory as" > "Test Sources root".
With the right click in the project window.
IDEA may be mislead by non-standard project layout.
Usually test libraries are of dependency type "test" and will not compile when not recognized as "test" code.
I am learning Scala so bear with me if this is a stupid question.
I have this package and a class (teared it down to most simplistic version):
package Foo {
class Bar {}
}
then in main.scala file I have:
import Foo.Bar
object test {
def main() {
val b = new Bar()
}
}
Why am I getting this:
test.scala:1: error: Bar is not a member of Foo
It points at the import statement.
scalac is the scala compiler. Foo.bar needs to have been compiled for you to use it, so you can't just run your main.scala as a script.
The other mistake in your code is that the main method needs to be
def main(args: Array[String]) { ...
(or you could have test extends App instead and do away with the main method).
I can confirm if you put the two files above in an empty directory (with the correction on the main method signature) and run scalac * followed by scala test it runs correctly.
The most likely explanation is that you did not compile the first file, or you are doing something wrong when compiling. Let's say both files are in the current directory, then this should work:
scalac *.scala
It should generate some class files in the current directory, as well as a Bar.class file in the Foo directory, which it will create.
To quickly test a scala code in IntelliJ (with the Scala plugin), you can simply type Ctrl+Shift+F10:
Note that for testing a Scala class, you have other choices, also supported in IntelliJ:
JUnit or TestNG
ScalaTest