使用 Specflow 场景进行集成测试和单元测试

发布于 2024-09-10 05:56:38 字数 214 浏览 0 评论 0原文

我刚刚接触到 BBD 和 Specflow,它看起来非常有趣。在编写用户故事时,他们通常处于较高的水平,并且参与者使用 GUI。因此,在编写场景时,它们通常是来自系统高层的 GUI 测试或集成测试。但是解决方案中进一步的单元测试又如何呢?例如,服务端点、业务对象等。我应该为这些编写新的场景,还是有办法重用相同的场景进行低级测试(单元测试),或者我应该复制并粘贴这些场景?

如果我弄错了,请告诉我。

I've just come across BBD and specflow and it looks very interesting. When writing user stories they are typically on a high-level and the actor users the GUI. So when writing scenarios they will typically be GUI test or integration test from a high level of the system. But what about unit test further down in the solution? E.g. service endpoints, business objects, etc. Should I write new scenarios for those or is there a way to reuse the same scenarios for low level testing (unit tests) or should I copy and past the scenarios?

Please let me know if I have got it all wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-09-17 05:56:38

像 SpecFlow 这样的 BDD 框架旨在帮助技术团队成员更轻松地与项目的非技术利益相关者进行沟通。

英语规范不容易维护或重构。由于阅读单元级测试或示例的人都是技术人员并且能够阅读代码,因此我更喜欢在该级别使用单元测试框架,例如 NUnit。

场景通常比单元测试复杂得多。通常我发现它们涵盖了内部业务逻辑的许多组合,并且组成组合的每个方面将由不同的代码单元负责。因此,场景中的逻辑将被分成许多不同的单元测试,并且您将无法复制它们。

有时我使用场景来指导我的单元测试。我可能会发现一些逻辑最终成为特定代码单元的责任,然后我可以将场景中的相关步骤作为注释复制到单元测试中。

// Given I have $100 in my account
var account = new Mock<Account>();
account.SetupGet(a => a.Balance).Returns(100);

var accountProvider = new Mock<AccountProvider>();
accountProvider.Setup(ap => ap.AccountFor("lunivore")).Returns(account);

// When I ask for $20
var atm = new ATM(accountProvider);
atm.PutInCardFor("lunivore");
int money = atm.RequestMoney(20);

// Then I should get $20
Assert.AreEqual(20, money);

// And my account should have paid out $20
account.verify(a => a.PayOut(20));

我鼓励您复制编写场景所用的语言(例如:PayOut)。这与领域驱动设计的“无处不在的语言”是一致的。将这种语言引入单元测试和代码中还可以帮助团队与利益相关者进行对话,因为您不必一遍又一遍地进行翻译。

将给定/何时/然后放入注释中也确实可以帮助我专注于交付实际将要使用的代码,而不是试图猜测所有边缘情况。最好质量的代码是您不编写的代码。

祝你好运!

BDD frameworks like SpecFlow are designed to help technical team members communicate more easily with non-technical stakeholders of a project.

The English-language specs aren't easy to maintain or refactor. Since the only people who read unit-level tests or examples are technical and able to read code, I prefer to use unit-testing frameworks such as NUnit at that level.

Scenarios often have far more complexity in them than unit tests. Normally I find that they cover a number of combinations of internal business logic, and each aspect which makes up a combination will be the responsibility of a different unit of code. The logic in the scenarios will therefore be split up amongst a number of different unit tests, and you won't be able to copy them.

Sometimes I use the scenarios to guide my unit tests. I might find that a bit of logic ends up being the responsibility of a particular unit of code, and then I can copy just the relevant steps from the scenario into the unit tests as a comment.

// Given I have $100 in my account
var account = new Mock<Account>();
account.SetupGet(a => a.Balance).Returns(100);

var accountProvider = new Mock<AccountProvider>();
accountProvider.Setup(ap => ap.AccountFor("lunivore")).Returns(account);

// When I ask for $20
var atm = new ATM(accountProvider);
atm.PutInCardFor("lunivore");
int money = atm.RequestMoney(20);

// Then I should get $20
Assert.AreEqual(20, money);

// And my account should have paid out $20
account.verify(a => a.PayOut(20));

I encourage you to copy the language in which the scenarios are written (eg: PayOut). This is aligned with Domain Driven Design's "ubiquitous language". Carrying that language into both unit tests and code also helps a team converse with stakeholders, because you won't have to do the translation over and over.

Putting Given / When / Then into comments also really helps me focus on delivering code that's actually going to be used, instead of trying to guess at all the edge cases. The best quality code is the stuff you don't write.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文