惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
V
Visual Studio Blog
小众软件
小众软件
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
IT之家
IT之家
Vercel News
Vercel News
C
Check Point Blog
Google DeepMind News
Google DeepMind News
月光博客
月光博客
D
DataBreaches.Net
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog

博客园 - PanPan003

AI coding - rewind、undo 撤销操作 如何省token? - 项目 知识 固化 如何省token?--- RTK ,input 数据压缩 如何省token?--- ignore文件 如何省token?--- token 用量统计 如何省token? - 何时开启新session? 如何省token? - 更便宜的模型 处理低风险任务 如何省token?-- 如何提问?避免直接粘贴 大段文件 如何省token?- Plan ,任务拆分,从小模型开始 如何省token?-rewind(todo) 如何省token?- handoff (todo) 如何省token? - compact,压缩上下文 Skill - superpowers skill - mattpocock OpenCode 安装 claude code 安装 npm config AI - coding - links/summary SSH 本地端口转发 --- VM中ubuntu service 可被 Windows 访问 内网穿透 - devtunnel Linux 虚拟机 反向代理 - 到宿主机 - decimal, double在 c#中的区别 python environment settings xUnit Theory: Working With InlineData, MemberData, ClassData docker network - container networking KEDA — Kubernetes Based Event Driven Auto scaling(转载) rabbit MQ —— ha-sync-mode. message 同步/ 丢失 in new pods rabbit MQ —— ha-mode, message 同步策列:所有nodes or one nodes 博文阅读密码验证 - 博客园 Kubernetes hpa
XUnit —— Record.Exception —— Stop Using Assert.Throws in ...
PanPan003 · 2023-05-08 · via 博客园 - PanPan003

原文:https://www.richard-banks.org/2015/07/stop-using-assertthrows-in-your-bdd.html

Stop Using Assert.Throws in Your BDD Unit Tests

I’m sure we’ve all seen the Assert.Throws assertion in test code by now, but just in case you haven’t, here's a simple example:

1

2

3

4

5

6

[Test]

public void InsertTestNameHere()

{

    var input = "a string";

    Assert.Throws<FormatException>(() => int.Parse(input));

}

If we consider this from an Arrange-Act-Assert (AAA) perspective it’s pretty easy to see that the Act and Assert logic are in a single line. It’s a very common pattern. It works, it works well, and the readability is fine, but if we start using a BDD approach to our unit testing (e.g. with SpecFlow) or we want to explicitly keep the Arrange, Act and Assert sections of our test code separated then Assert.Throws gets in the way.
To fix this we need a way of catching the exception and treating it like a normal object. In NUnit, the Assert.Catch method is for just this purpose. Here’s the adjusted code (using NUnit)

1

2

3

4

5

6

7

[Test]

public void InsertTestNameHere()

{

    var input = "a string";

    var exception = Assert.Catch(() => int.Parse(input));

    Assert.IsInstanceOf<FormatException>(exception);

}

In this case we’re catching any exception that int.Parse might throw. If we were more explicit and used Assert.Catch<FormatException>(), NUnit’s behaviour becomes much the same as Assert.Throws, and the test fails immediately if the expected exception isn’t detected. This isn't a behaviour we want, which is why we're using the generalised catch method. Now, since we have our exception in a variable, we can check if it’s the one we expected.
Oh, I forgot to mention it, but if no exception is thrown, Assert.Catch() fails the test immediately. It is an assertion after all. I’m sort of OK with this… but not really, as we’re still mixing assertions and actions on the same line and haven't resolved my original complaint.
For this reason I prefer XUnit’s approach.

1

2

3

4

5

6

7

8

[Fact]

public void InsertTestNameHere()

{

    var input = "a string";

    var exception = Record.Exception(() => int.Parse(input));

    Assert.NotNull(exception);

    Assert.IsType<FormatException>(exception);

}

The Record.Exception() method won't fail the test, regardless of what happens in the method. Unlike the NUnit approach, it merely records any exception arising from the call or returns null if no exception was thrown. Personally, I feel this approach is a better way to write tests around exceptions while still remaining consistent with the AAA approach. It also works well when using a BDD testing framework that separates the given/when/then step implementations as you can pass the result of the “when” method to the “then” methods with ease.