Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 使用MSTest的自定义断言-处理AssertFailedException_Unit Testing_Mstest - Fatal编程技术网

Unit testing 使用MSTest的自定义断言-处理AssertFailedException

Unit testing 使用MSTest的自定义断言-处理AssertFailedException,unit-testing,mstest,Unit Testing,Mstest,我想在我的MSTest单元测试框架中提供一些自定义断言方法。我有自己的静态类“CustomAssert”,方法如下: public static void DatesAreEqualToDay(DateTime expectedValue, DateTime actualValue) { if (!( expectedValue.Year == actualValue.Year && expectedValue.Month == actualV

我想在我的MSTest单元测试框架中提供一些自定义断言方法。我有自己的静态类“CustomAssert”,方法如下:

public static void DatesAreEqualToDay(DateTime expectedValue, DateTime actualValue)
{
    if (!(
       expectedValue.Year == actualValue.Year &&
       expectedValue.Month == actualValue.Month &&
        expectedValue.Day == actualValue.Day))
    {
        Assert.Fail(string.Format("Expected: <{0:yyyy-MM-dd}> - Actual <{1:yyyy-MM-dd}>", expectedValue, actualValue));
    }
}
公共静态无效日期ReequalToday(DateTime预期值、DateTime实际值)
{
如果((
expectedValue.Year==实际值.Year&&
expectedValue.Month==实际值.Month&&
expectedValue.Day==实际值.Day))
{
Assert.Fail(string.Format(“预期:-实际”、预期值、实际值”);
}
}

当日期不相同时,测试失败。但是,我得到的不是自定义错误消息,而是测试抛出AssertFailedException的消息。如何像MSTest的Assert方法一样处理自定义断言,并显示智能错误消息?

此问题是由于我的“TestFramework”项目引用的是VS2008版本的Microsoft.VisualStudio.QualityTools.UnitTesting程序集而导致的。我更改了对VS2010版本程序集的引用,自定义断言开始按预期运行,MSTest显示自定义断言消息,而不是“AssertFailedException”。

此问题是由于我的“TestFramework”导致的项目引用了Microsoft.VisualStudio.QualityTools.UnitTesting程序集的VS2008版本。我更改了对VS2010版本程序集的引用,自定义断言开始按预期运行,MSTest显示了自定义断言消息,而不是“AssertFailedException”。

您可以尝试使用当前可用的框架之一,而不是创建自己的自定义断言。例如:。

您也可以尝试使用当前可用的框架之一,而不是创建自己的自定义断言。例如: