How do you set the number of required succesful scalacheck tests - scalacheck

in munit scalacheck, how do I limit the number of inputs required to pass the test?

If your test extends munit.ScalaCheckSuite, there is a protected def scalaCheckTestParameters you can override to set the test parameters. A good way to set it is to modify the value in the base class, for example
class MyTestSuite extends munit.ScalaCheckSuite {
override val scalaCheckTestParameters = super.scalaCheckTestParameters.withMinSuccessfulTests(10)
property("my property") {
...
}
}

Related

How to prioritize tests in scala?

I am using scalatest's FunSuite to run my tests. I want to define 2 separate test classes that have a few tests in common, so I created a BaseTest class which I extended in MainTest1 and MainTest2, in which I define additional specific tests (see code snippets below). I want the tests in MainTest1 and MainTest2 to execute in a specific order, e.g. test 4, 2, 1 and then 3. How do I achieve this?
class BaseTest extends FunSuite with BeforeAndAfter{
test("Check Content of file){
//code to check content of a file
}
test("File is not empty"){
//code to check file is not empty
}
test("Check a particular word in file"){
//code to check particular word in file
}
}
class MainTest1 extends BaseTest{
test("Check create file"){
//code to check file creation
}
}
class MainTest2 extends BaseTest{
test("Check download file"){
//code to check file downloaded properly
}
}
You can define your tests as methods and call them in the order you see fit from the children classes.
trait BaseTest extends FunSuite {
protected def test1(): Unit = test("This is test 1") { ... }
protected def test2(): Unit = test("This is test 1") { ... }
}
class MainTest1 extends BaseTest {
// Arrange these is any order you see fit
test3()
test2()
test1()
protected def test3(): Unit = test("This is a test specific to MainTest1") { ... }
}
class MainTest2 extends BaseTest {
// Arrange these is any order you see fit
test2()
test3()
test1()
protected def test3(): Unit = test("This is a test specific to MainTest2") { ... }
}

How to create mock object of a class which is package private

I have a class. It has a companion object A with a factory method.
class A private[somepackage](x: Int) {
}
object A {
def createA(y: Int): A = {
new A(y)
}
}
Now I need to create the mock object of A in a scalatest file which is in a different package.
When I give
private val a = mock[A] --> I get compilation error.
constructor A in class A cannot be accessed in <<somewhere>>.
Is there a better way to mock the object ??
In your test sources, create a test double in the same package:
package somepackage
class MockableA extends A(0)
then just create a mock[MockableA] in your tests and continue as usual.
But the answer with a proxy/facade should work too if you are willing to change production sources to facilitate tests.
Consider using a proxy to access class A, and stub/mock that proxy class instead.
E.g., if A.doStuff is what you want to mock/stub, and A.accessStuff is what you need in your code, create a class
class ADecorated(underlying: A) {
def doStuff() {
underlying.doStuff()
// whatever I want to do
}
def accessStuff() {
x = underlying.accessStuff()
// do something else and return
}
// Any other method you want to use
}
Replace usage of A.createA with new ADecorated(A.createA()). ADecorated is what you work with now

Playframework 2.6 [Scala]: How to run my setup tests before any actual essential tests?

I want to know in what order the tests are running, because I want to run
my initial test setup tests only once before all the tests start running.
If I have one initial setup test class, and one essential test class it would be fine:
class EssentialTesting #Inject()(setupTests: SetupTest) extends ....{
setupTests.runInitialSetup()
.....
}
However if I have number of testings classes the setupTests.runInitialSetup will be repeated in each class. How to deal with this duplication?
Make your Setups class like:
class SetUpTest extends SpecificationLike with BeforeAndAfterAll{
override def beforeAll() = {
// runs before each test
}
override val afterAll() = {
// runs after each test
}
}
Now in your test file, you can extend your SetUpTest, like :
class Test extends PlaySpecification with SetupTest
Now you don't need to call the runinitialsetup every time, just put it into beforeAll method and it will get executed before each test. And SpecificationLike is required as BeforeAndAfterAll has self type reference for SpecificationLike.

Programmatically adding new tests via ScalaTest

I have a set of input cases stored in a file.
I would like each case to be a specific scalatest "test", i.e., reported in the console as an individual test and failed individually.
Unfortunately, experimentation and Google suggest that this capability might not be present?
E.g., this seems to be the common case (eliding for simplicity)
class MyTestingGoop extends FunSuite {
val input : Seq[SpecificTestCase] = ...
test("input data test") {
forAll(input) { case => ... }
}
//...
}
Ideally, each case presents as a separate test. How can this be done with ScalaTest?
You can do this:
class MyTestingGoop extends FunSuite {
val input : Seq[SpecificTestCase] = ...
forAll(input) {
test("testing input" + input) {
// do something with the test
}
}
}
The only limit is that input has a unique toString.
Basically calling test in Funsuite registers the test and later runs it so as long as your test creation is done as part of the class construction and each test has a unique string, you should be fine.

How to run ConfigMapWrapperSuite?

I need to write an integration test and it requires starting a server executable. I want to make location of the server configurable, so that I could set it on my box and on integration server.
ConfigMapWrapperSuite seems to be doing exactly what I want:
#WrapWith(classOf[ConfigMapWrapperSuite])
class ConsulTest(configMap: ConfigMap) extends FlatSpec with ShouldMatchers {
val consulPath = configMap("consul.path")
"Consul" should "list keys under root" in {
...
}
But when I set my IDE (IntelliJ) to execute all tests in the project, I get an exception saying that constructor with Map parameter not found. Looking into source code of scalatest revealed:
final class ConfigMapWrapperSuite(clazz: Class[_ <: Suite]) extends Suite {
private lazy val wrappedSuite = {
val constructor = clazz.getConstructor(classOf[Map[_, _]])
constructor.newInstance(Map.empty)
}
So in contrary to what documentation says, suite must have constructor with a Map and not ConfigMap.
Ok, I changed constructor to take a Map[String,String] but now I get NoSuchElementException at val consulPath = configMap("consul.path"). Lookung up the stack down to ConfigMapWrapperSuite and I see that constructor.newInstance(Map.empty) WTF? So wrapped suite class is instantiated with empty map, and than another time, during the suite run with actual map of parameters? How do I suppose to get parameters if I'm given an empty map?
I looked up scalatest's unit tests. They are so rudimentary that actually retrieving a value from configMap is not performed.
I do not want to use ConfigMapFixture because it will make me initializing every single test with the same code.
So, how do I not only pass but also get global setting in test suite?
Scalatest version: 3.0.0-M15
Ok, answering my own question. ConfigMapWrapperSuite seems to be not used too much and essentially is broken.
Instead I've used BeforeAndAfterAllConfigMap as in here:
class ConsulTest extends FlatSpec with ShouldMatchers with OneInstancePerTest with BeforeAndAfterAllConfigMap {
var consulProcess: Process = null
override def beforeAll(conf: ConfigMap): Unit = {
consulProcess = Seq("bin/"+exe, "agent", "-advertise", "127.0.0.1", "-config-file", "bin/config.json").run()
}
override def afterAll(conf: ConfigMap): Unit = {
consulProcess.destroy()
}
You need override instance
#WrapWith(classOf[ConfigMapWrapperSuite])
class AcceptanceTest(configMap: Map[String, Any])
extends FlatSpec
with OneInstancePerTest {
override def newInstance = new AcceptanceTest(configMap)
}