【问题标题】:Unit testing plugins in an application with plugin architecture具有插件架构的应用程序中的单元测试插件
【发布时间】:2013-09-07 21:45:08
【问题描述】:

我正在创建一个具有插件架构的应用程序,并且我想为插件创建通用测试,因为扩展点定义明确。插件将有自己的测试来测试内部的东西。

创建这些测试的首选方法是什么,以便每个插件都不需要包含针对“显而易见”内容的测试?我感兴趣的是,这些测试是如何与单元测试框架相关的。

如果有人指点我一个开源项目,它就足够了,它确实有这样的插件通用测试。我的应用程序是用 C#(以及用于组合的 MEF)制作的,因此首选使用类似(强类型)语言(如 Java)编写的项目。

【问题讨论】:

    标签: c# unit-testing plugins mef


    【解决方案1】:

    似乎将通用测试放入基类并为每个插件派生它是一种可行的方法:

    public interface IPluginComponent
    {
    }
    
    [TestClass]
    public abstract class BaseTests
    {
        protected abstract IPluginComponent CreateComponent();
    
        [TestMethod]
        public void SomeTest()
        {
            IPluginComponent component = this.CreateComponent();
    
            // execute test
        }
    }
    
    public class MyPluginComponent : IPluginComponent
    {
    }
    
    [TestClass]
    public class MyPluginTests : BaseTests
    {
        protected IPluginComponent CreateComponent()
        {
            return new MyPluginComponent();
        }
    
        [TestMethod]
        public void CustomTest()
        {
            // custom test
        }
    }
    

    但是,使用 MSTest 的测试应该注意一个错误,如果基类位于不同的程序集中,则无法在基类中运行测试。这在 Visual Studio 2010 中没有修复,不确定 2012:

    Shared Unit Tests with MSTest

    Visual Studio Test (MSTest) and lack of Inheritance support for base classes that resides in different assemblies

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      相关资源
      最近更新 更多