在TDD中,有Arrange Act Assert(AAA)语法:
[Test]
public void Test_ReturnItemForRefund_ReturnsStockOfBlackSweatersAsTwo_WhenOneInStockAndOneIsReturned()
{
//Arrange
ShopStock shopStock = new ShopStock();
Item blackSweater = new Item("ID: 25");
shopStock.AddStock(blackSweater);
int expectedResult = 2;
Item blackSweaterToReturn = new Item("ID: 25");
//Act
shopStock.ReturnItemForRefund(blackSweaterToReturn);
int actualResult = shopStock.GetStock("ID: 25");
//Assert
Assert.AreEqual(expectedResult, actualResult);
}
在BDD中,编写测试使用类似的结构,但语法为“当下(GWT)”:
[Given(@"a customer previously bought a black sweater from me")]
public void GivenACustomerPreviouslyBoughtABlackSweaterFromMe()
{ /* Code goes here */ }
[Given(@"I currently have three black sweaters left in stock")]
public void GivenICurrentlyHaveThreeBlackSweatersLeftInStock()
{ /* Code goes here */ }
[When(@"he returns the sweater for a refund")]
public void WhenHeReturnsTheSweaterForARefund()
{ /* Code goes here */ }
[Then(@"I should have four black sweaters in stock")]
public void ThenIShouldHaveFourBlackSweatersInStock()
{ /* Code goes here */ }
尽管它们通常被认为是相同的,但仍存在差异。一些关键的是:
GWT可以直接映射到BDD框架中功能文件的规范
通过鼓励使用通俗易懂的英语,并对每个部分的功能进行简短描述,GWT对于非开发人员来说更容易理解。
在各种BDD框架(例如SpecFlow和Cucumber)中给定“何时”和“之后”是关键字
我的问题是AAA和GWT之间是否还有其他区别(名称除外)?除了上面指定的理由之外,还有什么理由比另一个更可取吗?