如何在BDD中编写故事/场景(行为驱动设计)

时间:2023-01-19 22:20:56

I am about to use BDD (Behavior Driven Design) for the first time and am trying to get used to this different way of approaching a problem.

我将第一次使用BDD(行为驱动设计),并试图习惯这种不同的方法来解决问题。

Can you give some stories / scenarios that you would write for say a simple login application using BDD?

您能否提供一些故事/场景,您可以使用BDD说出一个简单的登录应用程序?

For example, from what I have read, it seems that is good:

例如,根据我的阅读,似乎很好:

When a user enters an invalid userid/password, then display an error message.

当用户输入无效的用户ID /密码时,则显示错误消息。

As opposed to:

相反:

Validate id and password by searching for a matching record in the database.

通过在数据库中搜索匹配的记录来验证ID和密码。

3 个解决方案

#1


Dan North has some excellent advice on writing stories. "Dan North- What's in a Story?"

Dan North在写故事方面有一些很好的建议。 “Dan North-故事中有什么?”

I would also take a look at some of the work being done around Cucumber as they have spent a lot of time thinking about how to write these stories in an understandable and executable way.

我还要看看Cucumber正在进行的一些工作,因为他们花了很多时间思考如何以可理解和可执行的方式编写这些故事。

#2


Dan North's article that _Kevin mentioned is great.

Dan North的文章_Kevin提到的很棒。

Remember, though, that there are "user stories," which should actually be written or at least collected from the client/users. These are more of the "As a , I want , so that " type stories.

但请记住,有“用户故事”,实际上应该写入或至少从客户端/用户收集。这些更像是“作为一种,我想要的,所以”类型的故事。

Then there are acceptance criteria, which identify how and when the user story will be said to be satisfied. That is like what you have written in your post: "When x, it should y."

然后是接受标准,其标识用户故事将如何以及何时被称为满足。这就像你在帖子中写的那样:“当x,它应该是y。”

There is a lot of overlap here with what I call "system stories" in my project management system and "specifications" in my tests which specify behaviour that the user may not be aware of, but describe interaction between your classes. System story: "When the LoginHandler is given a login and password, it should validate the data with a LoginValidator." Specification:

在我的项目管理系统中我称之为“系统故事”和我的测试中的“规范”有很多重叠,这些规范指定了用户可能不知道的行为,但描述了类之间的交互。系统故事:“当LoginHandler获得登录名和密码时,它应该使用LoginValidator验证数据。”规格:

[TestFixture]
public class When_the_login_handler_is_given_a_login_and_password
{
   constant string login = "jdoe";
   constant string password = "password";
   static LoginValidator loginValidator;

   context c = () => loginValidator = an<ILoginValidator>;

   because b = () => sut.Validate(login, password);

   it should_validate_the_data_with_a_LoginValidator =
      () => loginValidator.was_told_to(x => x.DoValidation(login, password));
}

Nevermind the testing syntax, you can see that the specification text itself is embodied in the test class name and method name. Furthermore, the test/spec is actually testing the behaviour of the classes. When tests like this pass for simple user stories, the acceptance criteria has been met.

没关系测试语法,您可以看到规范文本本身体现在测试类名称和方法名称中。此外,test / spec实际上是测试类的行为。当这样的测试通过简单的用户故事时,已经满足验收标准。

#3


I found an awesome talk here https://skillsmatter.com/skillscasts/2446-bdd-as-its-meant-to-be-done (caution, you need to create an account to view the video)

我在这里找到了一个很棒的演讲https://skillsmatter.com/skillscasts/2446-bdd-as-its-meant-to-be-done(小心,你需要创建一个帐户才能观看视频)

#1


Dan North has some excellent advice on writing stories. "Dan North- What's in a Story?"

Dan North在写故事方面有一些很好的建议。 “Dan North-故事中有什么?”

I would also take a look at some of the work being done around Cucumber as they have spent a lot of time thinking about how to write these stories in an understandable and executable way.

我还要看看Cucumber正在进行的一些工作,因为他们花了很多时间思考如何以可理解和可执行的方式编写这些故事。

#2


Dan North's article that _Kevin mentioned is great.

Dan North的文章_Kevin提到的很棒。

Remember, though, that there are "user stories," which should actually be written or at least collected from the client/users. These are more of the "As a , I want , so that " type stories.

但请记住,有“用户故事”,实际上应该写入或至少从客户端/用户收集。这些更像是“作为一种,我想要的,所以”类型的故事。

Then there are acceptance criteria, which identify how and when the user story will be said to be satisfied. That is like what you have written in your post: "When x, it should y."

然后是接受标准,其标识用户故事将如何以及何时被称为满足。这就像你在帖子中写的那样:“当x,它应该是y。”

There is a lot of overlap here with what I call "system stories" in my project management system and "specifications" in my tests which specify behaviour that the user may not be aware of, but describe interaction between your classes. System story: "When the LoginHandler is given a login and password, it should validate the data with a LoginValidator." Specification:

在我的项目管理系统中我称之为“系统故事”和我的测试中的“规范”有很多重叠,这些规范指定了用户可能不知道的行为,但描述了类之间的交互。系统故事:“当LoginHandler获得登录名和密码时,它应该使用LoginValidator验证数据。”规格:

[TestFixture]
public class When_the_login_handler_is_given_a_login_and_password
{
   constant string login = "jdoe";
   constant string password = "password";
   static LoginValidator loginValidator;

   context c = () => loginValidator = an<ILoginValidator>;

   because b = () => sut.Validate(login, password);

   it should_validate_the_data_with_a_LoginValidator =
      () => loginValidator.was_told_to(x => x.DoValidation(login, password));
}

Nevermind the testing syntax, you can see that the specification text itself is embodied in the test class name and method name. Furthermore, the test/spec is actually testing the behaviour of the classes. When tests like this pass for simple user stories, the acceptance criteria has been met.

没关系测试语法,您可以看到规范文本本身体现在测试类名称和方法名称中。此外,test / spec实际上是测试类的行为。当这样的测试通过简单的用户故事时,已经满足验收标准。

#3


I found an awesome talk here https://skillsmatter.com/skillscasts/2446-bdd-as-its-meant-to-be-done (caution, you need to create an account to view the video)

我在这里找到了一个很棒的演讲https://skillsmatter.com/skillscasts/2446-bdd-as-its-meant-to-be-done(小心,你需要创建一个帐户才能观看视频)