Scalatest problem : value FunSuite is not a member of org.scalatest - scala

I'm trying to write a test for my project in Scala 3. I added Scalatest library as :
libraryDependencies ++= Seq(
....
"org.scalatest" %% "scalatest" % "3.2.9" % Test
)
I know my structure is right:
But it gives me error:
value FunSuite is not a member of org.scalatest - did you mean scalatest.funsuite?
However, I used the same in another project and it works fine.

Thanks to #Johney
The correct usage is as below (at least in scala 3.0.2):
import org.scalatest.funsuite.*
class TestParser extends AnyFunSuite {
}
Of course the tutorials such as Getting started with FunSuite are based using import org.scalatest.FunSuite but the right examples is here which also referred as a first mention of FunSuite in Getting started with FunSuite .

Related

FunSuite missing even though ScalaTest is imported

I want to start writing simple tests in Scala using ScalaTest.
But for some reason, I can access org.scalatest but not org.scalatest.FunSuite
This is what my build.sbt looks like:
name := "Algorithms"
version := "0.1"
scalaVersion := "2.13.3"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0" % "test"
I don't understand if it can access scalatest then why are FunSuite,FlatSpec and other styles missing?
Output of running test on sbt shell
[error] <Project Path>\Algorithms\src\test\scala\Course1\Week1\MaxPairProductTest.scala:3:48: type FunSuite is not a member of package org.scalatest
[error] class MaxPairProductTest extends org.scalatest.FunSuite {
[error] ^
ScalaTest 3.2.0 has completed modularisation of the monolith from prior versions
The main change in ScalaTest 3.2.0 is carrying out the modularization
that we prepared for in 3.0.8 and 3.1.0. As a result, many deprecated
names have been removed, because the deprecations would cross module
boundaries.
This means that whilst in 3.1.0 the following definition
import org.scalatest.FunSuite
class ExampleSuite310 extends FunSuite {}
would just raise deprecation notice
The org.scalatest.FunSuite trait has been moved and renamed. Please use org.scalatest.funsuite.AnyFunSuite instead. This can be rewritten automatically with autofix: https://github.com/scalatest/autofix/tree/master/3.1.x", "3.1.0"
in 3.2.0 it has been removed entirely. Hence from 3.2.0 onwards you should define like so
import org.scalatest.funsuite.AnyFunSuite
class ExampleSuite320 extends AnyFunSuite {}
See deprecations expirations for full list of new names.
Note we can still import a single artifact which will pull transitively all the sub-artifacts
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0" % "test"
however now we also have the options of depenending on just the particular sub-artifact
libraryDependencies += "org.scalatest" %% "scalatest-funsuite" % "3.2.0" % "test"

How to find things and install them with Scala sbt?

I am new to Scala and struggling with sbt and installing things. For example:
I want to develop in Eclipse and hence use JUnit. According to:
http://www.scalatest.org/user_guide/using_junit_runner
"ScalaTest includes a JUnit Runner", that does not seem to be the case. At least not for me. (I get object is not a member of package org.scalatest when I try import org.scalatest.junit.JUnitRunner but import org.scalatest.flatspec.AnyFlatSpec works fine)
How do I install it? I looked at my sbt file, containing the lines:
libraryDependencies += "org.scalactic" %% "scalactic" % "3.1.1"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.1" % "test"
and thought OK so I need org.scalatest.junit and tried to add it like:
libraryDependencies += "org.scalatest" %% "junit" % "3.1.1"
which of course gave me three(!) screens of:
Note: Unresolved dependencies path:
[error] sbt.librarymanagement.ResolveException: Error downloading org.scalatest:junit_2.12:3.1.1
[error] Not found
(Why is it so repetitive? It's only one thing it hasn't found but it complains about it like four times!)
How do I figure out how to install things like this? Right now I am trying a combined approach of guesswork and random googling, sometimes it works and sometime (like now) it fails for me...
In ScalaTest 3.1.x to execute ScalaTest's suites using JUnit runner add the following dependencies to build.sbt
libraryDependencies ++= List(
"org.scalatest" %% "scalatest" % "3.1.1" % Test,
"org.scalatestplus" %% "scalatestplus-junit" % "1.0.0-M2" % Test
)
and annotate the suite like so
import org.junit.runner.RunWith
import org.scalatestplus.junit.JUnitRunner
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
#RunWith(classOf[JUnitRunner])
class HelloSpec extends AnyFlatSpec with Matchers {
"The Hello object" should "say hello" in {
Hello.greeting shouldEqual "hello"
}
}
There exists an open issue to update the documentation: Document how to use JUnitRunner in 3.x #1780

How come Scalatest can be used via Junit in Scala

In the build.sbt of an Example Assignment in a Scala course, the test library used is junit 4.10. There is no mention of Scalatest.
libraryDependencies += "junit" % "junit" % "4.10" % Test
And yet in a test class scalatest could be referenced and the tests could be written using actual scalatest syntax:
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
#RunWith(classOf[JUnitRunner])
class ListsSuite extends FunSuite with Matchers {
...etc...
}
Question: I suppose the Scala compiler accesses scalatest via the junit library. If so, what is the reason to embed scalatest in junit?
I checked the assignment project from the referred course and it actually has quite complicated structure using deprecated way of defining a build in project/*.scala files, extending Build.
The answer to your question is simple though: scalatest dependency is defined in project/CommonBuild.scala and added to the build in project/StudentBuildLike.scala. So there is no magic, you're using normal scalatest and a custom test runner (see project/ScalaTestRunner.scala).

Can't import scala.reflect.runtime.universe

I'd like to play around with reflection in scala (2.10.2) by following the example in
this tutorial. things work fine when I start the sbt (version 0.13) and import
scala.refelct.runtime.universe._
scala> import scala.reflect.runtime.universe._ │~
import scala.reflect.runtime.universe._
but when I try to put the sample code to an object like
object ReflectExample {
import scala.reflect.runtime.universe._
/*
the rest of the Example
*/
}
and compile the code by sbt compile I see the following error message like:
[error] object runtime is not a member of package reflect
[error] import scala.reflect.runtime.universe._
As explained in sbt's documentation, you need to add this line to the libraryDependencies field of your project in build.sbt:
"org.scala-lang" % "scala-reflect" % scalaVersion.value
You may want to try adding dependency to http://mvnrepository.com/artifact/org.scala-lang/scala-reflect

Running ScalaTest in Play Framework 2.2.X

I'm trying to run some functional ScalaTest in Play 2.2 but can't seem to be able to import de #Test annotation needed to run the tests. I've tried looking for solutions but they seem to be from different versions since none work with my 2.2 version.
Can anyone guide me on running a ScalaTest some on play?
The super basic test class I tried to run is:
class Test extends AssertionsForJUnit {
#Test def test() {
}
}
While the error I get is:
type mismatch; found : server.Test required:
scala.annotation.Annotation
Can you paste an example of your test class? Also you need to include ScalaTest in the build file:
libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.0" % "test"