具有多个测试用例的 Scalatest 或 Specs2
在 TestNg 和 Java 中,我们可以使用 DataProvider 运行多个测试用例,并且这作为单独的测试运行,这意味着测试的执行不会因失败而停止。有 ScalaTest 或 Specs/Specs2 的类似物吗?
In TestNg and Java, we can run multiple test cases using DataProvider, and this runs as separate tests, meaning execution of a test isn't stopped on failure. Is there an analogue for ScalaTest or Specs/Specs2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 ScalaTest 和 specs2 中,很容易在运行时创建测试用例,以便用数据对它们进行参数化。这是一个specs2的例子:
那么输出是:
而下面的规范:
将打印出类似的内容:
In both ScalaTest and specs2, it is easy to create test cases at run-time, in order to parameterize them with data. Here's an example with specs2:
Then the output is:
Whereas the following specification:
Will print out something like:
这个概念在 ScalaTest 中被称为“共享测试”,因为相同的测试代码被多个固定装置“共享”,其中“固定装置”是 TestNG 的 DataProvider 方法中的“数据”。对于 ScalaTest 中的每个样式特征,都有一种方法可以将测试表示为函数。以下是 WordSpec 的示例:
http://www.scalatest.org/scaladoc-1.6 .1/#org.scalatest.WordSpec@SharedTests
您也可以只使用 for 循环为不同的数据点注册相同的测试代码。这是在电子邮件讨论中提出的:
http://groups.google.com/group /scalatest-users/browse_thread/thread/7337628407b48064#
这种情况下的 for 循环代码如下所示:
这实际上注册了15 项测试,每个浏览器驱动程序有 5 项测试。我相信这就是你所追求的。
That concept is called "shared tests" in ScalaTest, because the same test code is being "shared" by multiple fixtures, where "fixtures" are the "data" in TestNG's DataProvider approach. There's a way to do this for each style trait in ScalaTest that expresses tests as functions. Here's an example for WordSpec:
http://www.scalatest.org/scaladoc-1.6.1/#org.scalatest.WordSpec@SharedTests
You can alternatively just use a for loop to register the same test code for different data points. This came up in an email discussion that's here:
http://groups.google.com/group/scalatest-users/browse_thread/thread/7337628407b48064#
The for loop code in that case looked like:
This actually registers 15 tests, five tests for each browser driver. This I believe is what you're after.
ScalaTest 提供表驱动的属性检查
使用此工具,您可以对不同的输入运行测试:
ScalaTest offers Table-driven property checks
Using this facility you can run a test for different inputs: