具有多个测试用例的 Scalatest 或 Specs2

发布于 2024-11-25 13:43:21 字数 118 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离笑几人歌 2024-12-02 13:43:21

在 ScalaTest 和 specs2 中,很容易在运行时创建测试用例,以便用数据对它们进行参数化。这是一个specs2的例子:

   class BasketSpecification extends Specification {

     "a basket must contain fruits" >> {
       Seq(apple, banana, orange) foreach { fruit => 
         ("it contains: " + fruit) >> {
           basket must contain(fruit)
         }
       }
     }
   }

那么输出是:

 A basket must contain fruits
 + it contains: apple
 + it contains: banana
 + it contains: orange

而下面的规范:

   class BasketSpecification extends Specification {

     "a basket must contain fruits" >> {
       Seq(apple, cake, orange) foreach { fruit => 
         ("it contains: " + fruit) >> {
           basket must contain(fruit)
         }
       }
     }
   }

将打印出类似的内容:

 A basket must contain fruits
 + it contains: apple
 x it contains: cake
   'basket' does not contain 'cake'
 + it contains: orange

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:

   class BasketSpecification extends Specification {

     "a basket must contain fruits" >> {
       Seq(apple, banana, orange) foreach { fruit => 
         ("it contains: " + fruit) >> {
           basket must contain(fruit)
         }
       }
     }
   }

Then the output is:

 A basket must contain fruits
 + it contains: apple
 + it contains: banana
 + it contains: orange

Whereas the following specification:

   class BasketSpecification extends Specification {

     "a basket must contain fruits" >> {
       Seq(apple, cake, orange) foreach { fruit => 
         ("it contains: " + fruit) >> {
           basket must contain(fruit)
         }
       }
     }
   }

Will print out something like:

 A basket must contain fruits
 + it contains: apple
 x it contains: cake
   'basket' does not contain 'cake'
 + it contains: orange
与酒说心事 2024-12-02 13:43:21

这个概念在 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 循环代码如下所示:

  for (browser <- List("IE", "Chrome", "Firefox")) { 
    test(browser + ": test one") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test two") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test three") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test four") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test five") { driver => 
      info("Testing using " + driver) 
    } 
  } 
} 

这实际上注册了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:

  for (browser <- List("IE", "Chrome", "Firefox")) { 
    test(browser + ": test one") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test two") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test three") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test four") { driver => 
      info("Testing using " + driver) 
    } 
    test(browser + ": test five") { driver => 
      info("Testing using " + driver) 
    } 
  } 
} 

This actually registers 15 tests, five tests for each browser driver. This I believe is what you're after.

岁月染过的梦 2024-12-02 13:43:21

ScalaTest 提供表驱动的属性检查
使用此工具,您可以对不同的输入运行测试:

   import org.scalatest.prop.TableDrivenPropertyChecks._

val fractions =
  Table(
    ("n", "d"),  // First tuple defines column names
    (  1,   2),  // Subsequent tuples define the data
    ( -1,   2),
    (  1,  -2),
    ( -1,  -2),
    (  3,   1),
    ( -3,   1),
    ( -3,   0),
    (  3,  -1),
    (  3,  Integer.MIN_VALUE),
    (Integer.MIN_VALUE, 3),
    ( -3,  -1)
  )
/*------------------------------------------------*/
import org.scalatest.matchers.ShouldMatchers._

forAll (fractions) { (n: Int, d: Int) =>

  whenever (d != 0 && d != Integer.MIN_VALUE
      && n != Integer.MIN_VALUE) {

    val f = new Fraction(n, d)

    if (n < 0 && d < 0 || n > 0 && d > 0)
      f.numer should be > 0
    else if (n != 0)
      f.numer should be < 0
    else
      f.numer should be === 0

    f.denom should be > 0
  }
}

ScalaTest offers Table-driven property checks
Using this facility you can run a test for different inputs:

   import org.scalatest.prop.TableDrivenPropertyChecks._

val fractions =
  Table(
    ("n", "d"),  // First tuple defines column names
    (  1,   2),  // Subsequent tuples define the data
    ( -1,   2),
    (  1,  -2),
    ( -1,  -2),
    (  3,   1),
    ( -3,   1),
    ( -3,   0),
    (  3,  -1),
    (  3,  Integer.MIN_VALUE),
    (Integer.MIN_VALUE, 3),
    ( -3,  -1)
  )
/*------------------------------------------------*/
import org.scalatest.matchers.ShouldMatchers._

forAll (fractions) { (n: Int, d: Int) =>

  whenever (d != 0 && d != Integer.MIN_VALUE
      && n != Integer.MIN_VALUE) {

    val f = new Fraction(n, d)

    if (n < 0 && d < 0 || n > 0 && d > 0)
      f.numer should be > 0
    else if (n != 0)
      f.numer should be < 0
    else
      f.numer should be === 0

    f.denom should be > 0
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文