使用 ScalaTest 测试多个数据集
是否有任何方便的方法使用 ScalaTest 对多个数据集执行测试 - 就像 JUnit 的 参数化测试?
Is there any convenient way execute tests on multiple data sets with ScalaTest — like in JUnit's parametrized tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ScalaTest 1.5 引入了用于测试多个数据集的表驱动的属性检查功能。
您混合(或导入其成员)
TableDrivenPropertyChecks
,然后您可以像这样定义表:将元组的 var arg 列表传递给
Table
。每个元组必须具有相同的元数,在本例中每个元组的元数为 4(4 个成员)。第一个元组都是字符串,它们定义了列的名称。随后的元组各自定义一行数据。您可以在元组中放置任何类型,但通常每列都包含相同的类型。不过,如果您愿意,您可以拥有可以包含任何内容的 Any 类型的列。您可以拥有包含 1 到 22 列的表格。如果您需要超过 22 列(Scala 中的最大元组大小当前为 22),您可以在一个或多个列中使用复合类型。有了表格后,您可以使用
forAll
检查它们,如下所示:forAll
采用两个参数列表。第一个是表,第二个是“属性函数”,它表达的东西对于表的每一行都应该是正确的。forAll
将获取表的每一行(当然,跳过列名称的标题行)并确保该属性成立。如果没有,您会收到一条不错的错误消息,说明表的哪一行失败、指定列的值是什么等等。Table
是一个Seq
数据元组,因此您也可以像Seq
一样使用它。例如,您可以获取Option[Exception]
的Seq
来指示哪些行失败,如下所示:生成的
Seq
包含一个Option
对于表中的每一行数据,如果该行的属性通过,则为None
;如果属性失败,则为Some[Exception]
。Some
中的异常包含有关失败的所有详细信息。ScalaTest 1.5 introduced the table-driven property checks feature for testing multiple data sets.
You mix in (or import the members of)
TableDrivenPropertyChecks
, then you can define tables like this:You pass a var arg list of tuples to
Table
. Each tuple has to have the same arity, in this case each tuple has arity 4 (4 members). The first tuple is all strings, and these define names for the columns. The subsequent tuples each define one row of data. You can put any type in the tuple, but in general each column would contain the same type. Though if you wanted you could have columns of type Any that can contain anything. You can have table with 1 to 22 columns. If you need more than 22 columns (the max tuple size in Scala is currently 22), what you could do is use a compound type in one or more columns.Once you have a table, you can check them with
forAll
like this:forAll
takes two parameter lists. The first is a table and the second is a "property function," which expresses something that should be true for every row of the table.forAll
will take each row of the table (skipping the heading row of column names, of course) and make sure the property holds. If it doesn't, you get a nice error message saying which row of the table failed, what the values of the named columns were, etc.A
Table
is aSeq
of the data tuples, so you can also use it like aSeq
. For example, you could get aSeq
ofOption[Exception]
indicating which rows failed like this:The resulting
Seq
contains oneOption
for each row of data in the table, which is aNone
if the property passed for that row, andSome[Exception]
if the property failed. The exception in theSome
contains all the details about the failure.数据驱动测试的另一种可能性是使用以下语法:
默认情况下测试并行执行,除非在 build.sbt 中设置
parallelExecution in Test := false
。Another possibility for data-driven testing is to use the following syntax:
Tests by default execute in parallel unless in build.sbt you set
parallelExecution in Test := false
.共享测试可能会让您感兴趣。它们允许您定义一些测试集,如这个堆栈示例:
然后在实际规范中,您可以像这样使用它:
所以换句话说,nonEmptyStack是参数化的测试集,您可以将其与不同的测试一起使用您想要测试的数据集。
Shared tests can be interesting to you. They allow you to define some set of tests like in this stack example:
and then in actual spec you can use it like this:
So in other words nonEmptyStack is parametrized set of tests that you can use with different data sets you want to test.