Search code examples
scalatestingscalatest

How to use Table tests on scala?


I'm trying to write a test with "N test cases" instead of a single test for every test case, I've found on google the Table class but when I try to add them to my test the class Table does not work, it keeps giving me the error:

Cannot resolve overloaded method 'Table'

The test:

import org.scalatest.prop.TableDrivenPropertyChecks

class PublishEventServiceSpec extends AsyncWordSpec
    with FixtureSupport
    with Matchers
    with AsyncMockFactory
    with TableDrivenPropertyChecks {


    "#my test cases" should {
        "test cases" in {
            val cases = Table(1, 2)
            forAll (cases) { c => assert(c != 0) }
        }
    }
}

The method forAll also gives me the same error "Cannot resolve overloaded method 'forAll'", how can I use the Table and forAll on my tests?

scalatest version: 3.0.8

scala version: 2.11.*


Solution

  • You were almost there. The issue here is that Table takes a name first, of type String, and then you can add rows. This is the apply method you are trying to use:

    def apply[A](heading : _root_.scala.Predef.String, rows : A*) : org.scalatest.prop.TableFor1[A] = { /* compiled code */ }
    

    When defining:

    val cases = Table(1, 2)
    

    1 will not be converted to String. In order to solve it, you need to add a name, for example:

    val cases = Table("name", 1,2)
    

    And the rest should be the same.