1

我正在尝试使用 Scala 使用 JUnit 或 ScalaTest 运行类的能力。我采用了为 FeatureSpec 给出的示例,并将其与使用 JUnitSuite 的示例相结合。与 JUnitSuite 结合时,我在输出(控制台)中生成 Scalatest.FeatureSpec 结构的给定/何时/然后输出时遇到问题。任何和所有反馈将不胜感激。需要知道这是否可能。提前致谢。

代码:

import collection.mutable.Stack
import org.junit.Test
import org.scalatest.junit.JUnitSuite
import org.scalatest.matchers.MustMatchers
import org.scalatest.{GivenWhenThen, FeatureSpec}

class ExampleSpecTest extends FeatureSpec with JUnitSuite with GivenWhenThen with MustMatchers
{

@Test
 def featureSpecExample() {
   feature("The user can pop an element off the top of the stack") {

     info("As a programmer")
     info("I want to be able to pop items off the stack")
     info("So that I can get them in last-in-first-out order")

     scenario("pop is invoked on a non-empty stack") {

       given("a non-empty stack")
       val stack = new Stack[Int]
       stack.push(1)
       stack.push(2)
       val oldSize = stack.size

       when("when pop is invoked on the stack")
       val result = stack.pop()

       then("the most recently pushed element should be returned")
       result must be === 2

       and("the stack should have one less item than before")
       stack.size must be === oldSize - 1
     }

     scenario("pop is invoked on an empty stack") {

       given("an empty stack")
       val emptyStack = new Stack[String]

       when("when pop is invoked on the stack")
       then("NoSuchElementException should be thrown")
       evaluating { emptyStack.pop() } must produce [NoSuchElementException]

       and("the stack should still be empty")
       emptyStack must be ('empty)
     }
   }
 }
}

当我运行这个版本的 ExampleSpecTest 时,我得到以下输出:

Run starting. Expected test count is: 1
ExampleSpecTest:
Test Starting - ExampleSpecTest: featureSpecExample
Test Succeeded - ExampleSpecTest: featureSpecExample
Run completed in 255 milliseconds.
Total number of tests run: 1
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 0, ignored 0, pending 0
All tests passed.

我没有在报告中得到任何预期的给定/何时/然后输出

预期的:

Run starting. Expected test count is: 1
ExampleSpecTest:
Test Starting - ExampleSpecTest: featureSpecExample
Feature: The user can pop an element off the top of the stack  
   As a programmer  
   I want to be able to pop items off the stack  
   So that I can get them in last-in-first-out order  
   Scenario: pop is invoked on a non-empty stack
      Given a non-empty stack    
      When when pop is invoked on the stack    
      Then the most recently pushed element should be returned    
      And the stack should have one less item than before  
   Scenario: pop is invoked on an empty stack
      Given an empty stack    
      When when pop is invoked on the stack    
      Then NoSuchElementException should be thrown    
      And the stack should still be empty
Test Succeeded - ExampleSpecTest: featureSpecExample Run completed in 96 milliseconds.
Total number of tests run: 2
Suites: completed 1, aborted 0
Tests: succeeded 1, failed 0, ignored 0, pending 0
All tests passed.
4

0 回答 0