为什么 Jasmine 被称为 "BDD" 测试框架,即使不支持 "Given/When/Then"?
Why Jasmine is called a "BDD" testing framework even if no "Given/When/Then" supported?
在Jasmine的介绍中说:
Jasmine is a behavior-driven development framework for testing JavaScript code.
看了好几篇BDD的文章,好像应该用'Given/When/Then'来定义"Scenario","cucumber"就是这么做的。但是在Jasmine中,我看不到任何这样的方法。
即使 Jasmine 没有这样的概念,我们还能称其为 "BDD" 测试框架吗?
我将 Jasmine 描述为 javascript 的单元测试框架,它具有大量语法糖,因此我们可以将 'tests' 编写为描述行为的规范。大多数情况下,我们使用 Given、When、Then when describing/specifying 以业务语言表示系统的整体行为。当您描述系统的组件时,使用自然语言通常好处较少 - 编程语言更好地支持较低抽象级别所需的更多细节。
现在单元测试是 BDD 的基本组成部分,jasmine 以一种允许我们相当雄辩地指定行为的方式支持这一点,所以是的,它绝对可以是 BDD 测试 tool/framework 即使它只针对较低的抽象层次。
Cucumber 只针对更高层次的抽象。事实上,它在编写低级规范方面的努力并没有使它成为 BDD 测试框架(BDD 协作工具)。
有趣的一点是,要进行 BDD,您确实需要两种不同的测试工具,一种用于执行高级抽象,另一种用于执行较低级别的详细规范。用于不同任务的不同工具都是同一流程的一部分。
最后GWT真的只是一个写场景的实现细节。这是区分
的一种方式
- 正在设置状态 G
- 描述一个动作W
- 检查操作 T
的后果
Jasmine 不会阻止您使用 given-when-then,下面的示例显示了在使用 Jasmine 时可以使用 given-when-then 的两种方式。
describe("Given a string containing 'foo'", function(){
var someString;
beforeEach(function() {
someString = "foo";
});
describe("When I append 'bar'", function(){
beforeEach(function() {
someString += "bar";
});
it("Then the string is 'foobar'", function(){
expect(someString).toBe("foobar");
});
});
it("When I append 'baz' Then the string is 'foobaz'", function(){
someString += "baz";
expect(someString).toBe("foobaz");
});
});
找到适合您的风格。您应该确保测试描述有效地描述了您正在测试的内容。您可以使用 given-when-then 风格的句子作为一种工具,以确保您的测试描述准确地描述了正在测试的内容。
回复@PDHide的问题:在Jasmine中是允许的。
Hi how can you use nested describe ? As per Jasmin , describe is for the testsuite and it for testcases
The docs 说:
Nesting describe Blocks
Calls to describe
can be nested, with specs defined at any level. This allows a suite to be composed as a tree of functions. Before a spec is executed, Jasmine walks down the tree executing each beforeEach
function in order. After the spec is executed, Jasmine walks through the afterEach
functions similarly.
也说:
The describe
function is for grouping related specs [...]
在Jasmine的介绍中说:
Jasmine is a behavior-driven development framework for testing JavaScript code.
看了好几篇BDD的文章,好像应该用'Given/When/Then'来定义"Scenario","cucumber"就是这么做的。但是在Jasmine中,我看不到任何这样的方法。
即使 Jasmine 没有这样的概念,我们还能称其为 "BDD" 测试框架吗?
我将 Jasmine 描述为 javascript 的单元测试框架,它具有大量语法糖,因此我们可以将 'tests' 编写为描述行为的规范。大多数情况下,我们使用 Given、When、Then when describing/specifying 以业务语言表示系统的整体行为。当您描述系统的组件时,使用自然语言通常好处较少 - 编程语言更好地支持较低抽象级别所需的更多细节。
现在单元测试是 BDD 的基本组成部分,jasmine 以一种允许我们相当雄辩地指定行为的方式支持这一点,所以是的,它绝对可以是 BDD 测试 tool/framework 即使它只针对较低的抽象层次。
Cucumber 只针对更高层次的抽象。事实上,它在编写低级规范方面的努力并没有使它成为 BDD 测试框架(BDD 协作工具)。
有趣的一点是,要进行 BDD,您确实需要两种不同的测试工具,一种用于执行高级抽象,另一种用于执行较低级别的详细规范。用于不同任务的不同工具都是同一流程的一部分。
最后GWT真的只是一个写场景的实现细节。这是区分
的一种方式- 正在设置状态 G
- 描述一个动作W
- 检查操作 T 的后果
Jasmine 不会阻止您使用 given-when-then,下面的示例显示了在使用 Jasmine 时可以使用 given-when-then 的两种方式。
describe("Given a string containing 'foo'", function(){
var someString;
beforeEach(function() {
someString = "foo";
});
describe("When I append 'bar'", function(){
beforeEach(function() {
someString += "bar";
});
it("Then the string is 'foobar'", function(){
expect(someString).toBe("foobar");
});
});
it("When I append 'baz' Then the string is 'foobaz'", function(){
someString += "baz";
expect(someString).toBe("foobaz");
});
});
找到适合您的风格。您应该确保测试描述有效地描述了您正在测试的内容。您可以使用 given-when-then 风格的句子作为一种工具,以确保您的测试描述准确地描述了正在测试的内容。
回复@PDHide的问题:在Jasmine中是允许的。
Hi how can you use nested describe ? As per Jasmin , describe is for the testsuite and it for testcases
The docs 说:
Nesting describe Blocks
Calls to
describe
can be nested, with specs defined at any level. This allows a suite to be composed as a tree of functions. Before a spec is executed, Jasmine walks down the tree executing eachbeforeEach
function in order. After the spec is executed, Jasmine walks through theafterEach
functions similarly.
也说:
The
describe
function is for grouping related specs [...]