如何编写 NUnit 单元测试而不必用 try catch 语句包围它们?
在我的公司,我们正在编写一堆单元测试。 我们想要做的是执行单元测试,并且每当测试结束时成功或失败时,我们都可以将其写入某处,但我们不想在每个测试中都放入该逻辑。
知道我们如何编写测试而不必使用我们一直在使用的 try catch 逻辑包围测试内容吗?
At my company we are writing a bunch of unit tests. What we'd like to have done is for the unit tests to execute and whenever one succeeds or fails at the end of the test we can write that somewhere but we don't want to put that logic in every test.
Any idea how we could just write tests without having to surround the content of the test with the try catch logic that we've been using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我猜你会做这样的事情:
没有必要这样做。 如果抛出异常,测试将自动失败。 例如,以下测试将显示为失败:
I'm guessing you do something like this:
There is no need for this. The tests will fail automatically if they throw an exception. For example, the following test will show up as a failure:
我不完全确定你想在这里做什么。 您是说您将其包装在 try/catch 中,以便可以在发生异常时捕获并记录该异常吗?
如果是这样,那么更好的方法可能就是让 NUnit 编写一个输出文件并使用它。 我已经有大约一年没有使用 NUnit 了,但是 IIRC 您可以使用 /out 指令将其输出重定向到您喜欢的任何文件。
如果您有理由必须按照您所说的方式记录它,那么您要么必须将自定义代码添加到每个测试中,要么有一个通用的“运行程序”来接受您的代码(对于每个测试)作为匿名方法并在单个 try..catch 中运行它。 这将防止您必须为每个测试重复 try..catch 。
如果我误解了这个问题,请道歉。
I'm not entirely sure what you are trying to do here. Are you saying you are wrapping it in a try/catch so that you can catch when an exception occurs and log this?
If so, then a better way, probably, is just to get NUnit to write an output file and use this. I haven't used NUnit for about a year, but IIRC you can redirect its output to any file you like using the /out directive.
If there is a reason why you have to log it the way you say, then you'll either have to add your custom code to each test, or have a common "runner" that takes your code (for each test) as an anonymous method and runs it inside a single try..catch. That would prevent you having to repeat the try..catch for every test.
Apologies if I've misunderstood the question.
MSTest 有 TestCleanup,它在每次测试后运行。 在 NUnit 中,要使用的属性是 TearDown (每次测试后)或 TestFixtureTearDown (毕竟测试完全)。 这在每个测试结束后执行。
如果您希望在测试通过时运行某些内容,则可以使用成员变量 shouldRunExtraMethod,该变量在每次测试之前初始化为 false,并在测试结束时更改为 true。 并且在 TearDown 上,您仅根据此变量值执行它
MSTest has TestCleanup, which runs after every test. In NUnit, the attribute to be used is TearDown (after every test) or TestFixtureTearDown (after all the test are completely). This executes after the end of each test.
If you want something to run just in case a test passes, you could have a member variable shouldRunExtraMethod, which is initialized to false before each test, and is changed to true at the end of the test. And on the TearDown, you only execute it depending on this variable value
如果您的单元测试方法涵盖了您期望引发异常的场景,请使用
ExpectedException
属性。 这里有一篇关于使用该属性的帖子。nUnit 中的预期异常...
If your unit test method covers the scenario in which you expect exceptions to be thrown, use the
ExpectedException
attribute. There's a post here on SO about using that attribute.Expect exceptions in nUnit...
NUnit 断言语句都有一个选项,可以在每次测试失败时打印一条消息。
尽管如果您想让它在每个测试结束时在某个地方写出一些内容,您可以在每个方法的拆卸中进行设置。 只需将字符串设置为您想要在测试本身中写入的内容,然后在拆卸过程中(每次测试后发生)它就可以用它做任何您想做的事情。
我相当确定即使抛出异常也会发生拆卸。 那应该可以达到你想要的效果。
NUnit assert statements all have an option to print a message for each test for when it fails.
Although if you'd like to have it write out something somewhere at the end of each test, you can set it up in the teardown of each method. Just set the string to what you want written inside the test itself, and during teardown (which happens after each test) It can do whatever you want with it.
I'm fairly certain teardown occurs even if an exception is thrown. That should do what you're wanting.
您遇到的问题是,只要断言失败,NUnit Assert.* 方法就会抛出 AssertionException - 但它不会执行其他任何操作。 因此,您似乎无法检查单元测试之外的任何内容来验证测试是否失败。
我能想到的唯一替代方案是使用 AOP(面向方面编程)和 PostSharp 等工具。 该工具允许您创建可以对某些事件起作用的方面。 例如:
此方面是每当引发异常时运行的代码:
由于上述测试将引发异常,因此 ExceptionDialogAttribute 中的代码将运行。 您可以获得有关该方法的信息,例如其名称,以便可以将其记录到文件中。
我已经很长时间没有使用 PostSharp 了,因此值得查看示例并进行试验。
The problem you have is that the NUnit Assert.* methods will throw an AssertionException whenever an assert fails - but it does nothing else. So it doesn't look like you can check anything outside of the unit test to verify whether the test failed or not.
The only alternative I can think of is to use AOP (Aspect Oriented Programming) with a tool such as PostSharp. This tool allows you to create aspects that can act on certain events. For example:
This aspect is code which runs whenever an exception is raised:
Since the above test will raise an exception, the code in ExceptionDialogAttribute will run. You can get information about the method, such as it's name, so that you can log it into a file.
It's been a long time since I used PostSharp, so it's worth checking out the examples and experimenting with it.