Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top