Skip to main content
 首页 » 编程设计

c#之Microsoft Fakes 测试框架与 MS 单元测试框架有什么区别

2024年02月06日16落叶

Fakes 测试框架可能仅适用于 Visual Studio 的高级版和旗舰版。 但是,与 MS 单元测试框架相比,还有其他区别吗?

请您参考如下方法:

您使用 MS 的单元测试框架在 Visual Studio 中编写单元测试。这是您可以用作指南的基本类:

using System; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
 
namespace UnitTestProject1 
{ 
    [TestClass] 
    public class UnitTest1 
    { 
        [TestMethod] 
        public void TestMethod1() 
        { 
            // Arrange 
 
            // Act 
 
            // Assert 
        } 
    } 
} 

如果您发现您的单元测试有很多依赖项,您会使用 Microsoft Fakes 之类的东西来隔离您真正需要测试的代码片段。如果您正在使用 TDD,并且您正在单元测试依赖于其他未完成代码的代码,您也可以使用 Fakes 来模拟不完整的代码。

来自 Isolating Code Under Test with Microsoft Fakes :

Fakes come in two flavors:

A stub replaces a class with a small substitute that implements the same interface. To use stubs, you have to design your application so that each component depends only on interfaces, and not on other components. (By "component" we mean a class or group of classes that are designed and updated together and typically contained in an assembly.)

A shim modifies the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you cannot modify, such .NET assemblies.

您在单元测试中使用 Microsoft Fakes。

这是 Microsoft Fakes 的重要资源: http://vsartesttoolingguide.codeplex.com/releases/view/102290