3

I'm trying to dynamically run some basic tests in the following way (this is a pseudo code of my test, my actual tests are a bit more complicated):

class ExampleTest extends AnyWordSpec {
  
  def runTests() = {
    for( n <- 1 until 10){
      testNumber(n)
    }
  }
  
  def testNumber(n: Int) = {
    "testNumber" when {
      s"gets the number ${n}" should {
        "fail if the number is different than 0" {
          assert(n == 0)
        }
      }
    }
  }
  
  runTests()
  
}

When I try to run my tests in IntelliJ all the tests run as expected. But when using sbt tests It says that all my tests passed even though non of my tests actually got executed (I get the message "1 tests suite executed. 0 tests where executed"). How can I fix this? Is there any other simple way to dynamically create tests in scala?)

dod haim
  • 31
  • 1
  • Maybe this is useful: https://stackoverflow.com/questions/29972284/scalatest-on-sbt-not-running-any-tests – Jeppe Nov 07 '21 at 08:17
  • 2
    You may want to use Scalatest `forall` feature. See https://stackoverflow.com/questions/68041007/scalatest-create-dynamic-tests-based-on-input-expected-output-forall for instance. – Gaël J Nov 07 '21 at 09:46
  • Unclear what's going on. I created a small project with just this tests, using Java 11, Scala 2.13.6, Sbt 1.4.2 and pulling in Scalatest 3.2.10 and `sbt test` successfully runs the tests. It's probably just a typo, but if by any chance you have some custom build key called `tests` that could be the reason for the behavior you are observing (the command you want to invoke is `sbt test`). – stefanobaghino Nov 07 '21 at 12:30

1 Answers1

3

As mentioned in a comment, I'm not sure what the problem with sbt is.

Another way you can try to create test dynamically is using property-based testing.

One possible way is using table-driven property checks, as in the following example (which you can see in action here on Scastie):

import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.prop.TableDrivenPropertyChecks._

class ExampleTest extends AnyWordSpec {

  private val values = Table("n", (1 to 10): _*)

  "testNumber" when {
    forAll(values) { n =>
      s"gets the number ${n}" should {
        "fail if the number is different than 0" in {
          assert(n == 0)
        }
      }
    }
  }

}

Of course all these tests are going to fail. ;-)

Links to the ScalaTest documentation about the topic:

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63