[英]Assert in Try..Catch block is caught
Just came across some interesting behavior - Assert
being caught by Catch
block. 刚遇到一些有趣的行为-
Assert
被Catch
块Catch
。
List<Decimal> consArray = new List<decimal>();
try
{
Decimal d;
Assert.IsTrue(Decimal.TryParse(item.Value, out d));
consArray.Add(d);
}
catch (Exception e)
{
Console.WriteLine(item.Value);
Console.WriteLine(e);
}
Assert throws AssertFailedException
and its caught by catch
. Assert抛出
AssertFailedException
并被catch
。 Always thought that if Assert
fails then test is failed and consecutive execution is aborted. 一直认为,如果
Assert
失败,则测试将失败,并且连续执行将中止。 But in that case - test moves along. 但在那种情况下-测试会继续进行。 If nothing wrong happens later - I get green test!
如果以后没有发生任何错误-我会通过绿色测试! In theory - is it right behavior?
从理论上讲-这是正确的行为吗?
Edited: I understand that maybe it is .NET restriction and how asserts are made in MsTest. 编辑:我知道也许是.NET限制以及MsTest中如何进行断言。 Assert throws exception.
断言引发异常。 Since
catch
- catches everything it catches assert exception. 由于
catch
捕获其捕获的所有内容,因此会断言断言异常。 But is it right in theory or MsTest specific? 但是理论上正确还是MsTest特定?
As already answered, this is correct behavior. 正如已经回答的那样,这是正确的行为。 You can change your code to get Your expected behavior by catching the AssertFailedException and re-throwing it.
你可以改变你的代码通过捕捉AssertFailedException并重新把它扔得到您预期的行为。
List<Decimal> consArray = new List<decimal>();
try
{
Decimal d;
Assert.IsTrue(Decimal.TryParse(item.Value, out d));
consArray.Add(d);
}
catch (AssertFailedException)
{
throw;
}
catch (Exception e)
{
Console.WriteLine(item.Value);
Console.WriteLine(e);
}
Your code is working as expected. 您的代码按预期工作。 When an
Assert
fails it throws an AssertFailedException which inherits from Exception . Assert
失败时,将引发从Exception继承的AssertFailedException 。 So you can add a try-catch
and catch it. 因此,您可以添加一个
try-catch
并捕获它。
In your case, add a throw
at the end of the catch
and re-throw the exception. 在你的情况下,添加一个
throw
在年底catch
并重新抛出异常。
NUnit
will do the exact same thing. NUnit
会做完全相同的事情。 As should any other test framework I think, but I only know MStest
and NUnit
in C#. 就像我认为的任何其他测试框架一样,但是我只知道C#中的
MStest
和NUnit
。
I'd expect that your test code would not contain Decimal.TryParse
, but your business logic would do that, which you'd test with an object and a method call. 我希望您的测试代码不会包含
Decimal.TryParse
,但是您的业务逻辑会做到这一点,您将使用对象和方法调用进行测试。
Something like: 就像是:
var sut = new Sut();
var d = sut.DoSomethingThatReturnsTheDecimal(item.Value);
Assert.AreEqual(0.123, d, string.Format("passed value can not be parsed to decimal ({0})", item.Value);
In order to stay a bit closer to your implementation: 为了更接近您的实现:
List<Decimal> consArray = new List<decimal>();
Decimal d = Decimal.MinValue;
// You don't need to try-catch a Decimal.TryParse
// Decimal.TryParse(item.Value, out d));
try
{
d = Decimal.Parse(item.Value)
}
catch
{
// Handle exception
}
Assert.AreEqual(0.123, d);
// Does the list add anything at all? In this sample it seems a bit redundant
consArray.Add(d);
Anyway, to answer your question. 无论如何,回答您的问题。 The try-catch is supposed to catch your
AssertFailedException
. try-catch应该捕获您的
AssertFailedException
。
PS: Catching the AsserFailedException
and re-throwing it will also work, but it feels a bit strange to me. PS:捕获
AsserFailedException
并将其重新抛出也可以,但是对我来说有点奇怪。 I'd strive to leave the Assert
s outside any try-catch
blocks. 我努力将
Assert
s保留在任何try-catch
块之外。 But that might be just my opinion which you didn't ask for :). 但这可能只是我的意见,您并没有要求:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.