Jih*_*Han 3 tdd bdd unit-testing coding-style
在很多情况下,我很难为类和方法提供一个好的单元测试名称.通常,我尝试遵循以下形式:
public class TestContext
{
[Fact]
public void WhenThis_DoThat()
{
}
}
Run Code Online (Sandbox Code Playgroud)
有些人把词语给出,当时,然后在部分上明确.我喜欢它,因为它似乎使单元测试更清楚它正在测试它是什么.除了考虑BDD工具包之外,我还需要一些关于如何使用普通旧xUnit工具的建议.
我有这样的场景特别困难:
当应用程序启动时,主窗体加载,用户可以看到用户可以单击的链接列表.
或者更好的用例场景是:
用户可以从链接列表中选择一个链接.
我不确定,但我正在尝试描述运行应用程序的行为,并且表单加载了可点击链接列表.并将其转变为单元测试.
什么是给定,何时,然后呢?
以下是我用BDD规范样式编写测试的方法:http://github.com/orfjackal/tdd-tetris-tutorial
需要的是能够在一个类中拥有多个测试装置.然后可以组织测试,每个夹具是"给定/何时"部分,每个测试方法是"然后"部分.JUnit不支持它,所以我为它写了一个简单的测试运行器:
@RunWith(NestedJUnit4.class)
public class WerewolfTest extends Assert {
public class Given_the_moon_is_full {
@Before public void When_you_walk_in_the_woods() {
...
}
@Test public void Then_you_can_hear_werewolves_howling() {
...
}
@Test public void Then_you_wish_you_had_a_silver_bullet() {
...
}
}
public class Given_the_moon_is_not_full {
@Before public void When_you_walk_in_the_woods() {
...
}
@Test public void Then_you_do_not_hear_any_werewolves() {
...
}
@Test public void Then_you_are_not_afraid() {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4084 次 |
| 最近记录: |