1

It looks to me that testing has come a long way since SUnit, JUnit, and the common xUnit test frameworks.

I looked into RSpec and Cucumber to get an idea what 'behavior-driven' testing is about, but now I do wonder which kind of other approaches to writing unit-tests exist.

I am not tied to any particular language, but more interested in general ideas and approaches to test code.

So, my question is, which stylistically different approaches are there to unit testing, i.e., approaches different from "asserts".

Let me give examples:

The xUnit style of unit-testting looks roughly like this in your favorit language:

stack = new Stack()
assertTrue  (stack.empty())

stack.push(1)
assertFalse (stack.empty())
assertEquals(1, stack.top())

RSpec-style unit-testting looks rather like this (roughly improvised):

describe Stack do

  it "should be empty" do
    Stack.new.empty?.should == true
  end

  it "should contain new value on top" do
    stack = Stack.new
    stack.push(1)
    stack.empty?.should == false
    stack.top().should == 1
  end
end

Are there other interesting styles, notations, 'DSLs'? Sure, every language and framework will have variations on the theme, but are there different general 'themes'/styles?

4

2 回答 2

1

我只能想到两种类型的测试。以代码为中心(xUnit 和 RSpec 给出了上面的代码)和以行为为中心(黄瓜,SpecFlow - Given/Then/When)。现在,您可以在以代码为中心的框架(代码 #1)中进行行为样式测试。

您还可以进行三种类型的测试:

  • Mockists - 大多数依赖项都是(严格)模拟的。
  • Stubbists - 大多数依赖项都是存根的。
  • 集成 - 大多数依赖项都是真实的。

代码#1:

@Test
public void theScenarioDescription() {
    givenThatSomething();
    whenIWiggleIt();
    thenSomethingHasHappened();
}
于 2012-07-24T16:07:28.680 回答
0

就我个人而言,我真的很喜欢 Given、When、Then 类型的测试语法。

Given some particular situaion
When some action occurs
Then there will be an outcome that can be verified.

我开始使用这种方法,使用.NET 上的StoryQSpecFlow等工具,它们非常整洁。从那以后我开始编写自己的小框架 ,它更适合我自己的个人风格,但还不是特别成熟。

于 2012-07-21T17:35:11.777 回答