如何在 Scala 上使用 Table 测试?

How to use Table tests on scala?

我正在尝试用“N 个测试用例”编写一个测试,而不是为每个测试用例编写一个测试,我在 google Table class 上找到了但是当我尝试将它们添加到我的测试时 class Table 不起作用,它一直给我错误:

Cannot resolve overloaded method 'Table'

测试:

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

方法 forAll 也给我同样的错误“无法解析重载方法 'forAll'”,我如何在测试中使用 TableforAll

最新版本:3.0.8

scala 版本:2.11.*

你快到了。这里的问题是 Table 首先取一个名称,类型为 String,然后您可以添加行。这是您尝试使用的应用方法:

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

定义时:

val cases = Table(1, 2)

1 不会转换为 String。为了解决它,你需要添加一个名称,例如:

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

其余同理