Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamicaly Created Tests sbt

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?)

like image 339
dod haim Avatar asked May 15 '26 11:05

dod haim


1 Answers

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:

  • property-based testing
  • table-driven property checks
  • generator-driven property checks (especially useful to test a lot of cases within the same case)
like image 145
stefanobaghino Avatar answered May 19 '26 04:05

stefanobaghino