【问题标题】:How to programmatically run unit tests with Gallio and MBUnit?如何使用 Gallio 和 MBUnit 以编程方式运行单元测试?
【发布时间】:2014-01-29 10:04:49
【问题描述】:

我正在尝试以编程方式检查我的单元测试是否作为部署过程的一部分通过。该应用程序使用 MBunit 和 Gallio 作为其单元测试框架。

这是我的代码:

var setup = new Gallio.Runtime.RuntimeSetup();
setup.AddPluginDirectory(@"C:\Program Files\Gallio\bin");

using (TextWriter tw = new StreamWriter(logFilename))
{
    var logger = new Gallio.Runtime.Logging.TextLogger(tw);
    RuntimeBootstrap.Initialize(setup, logger);

    TestLauncher launcher = new TestLauncher();                
    launcher.AddFilePattern(dllToRunFilename);
    TestLauncherResult result = launcher.Run();
}

这是我正在加载的 DLL 中包含的测试(我已经验证了它可以与 Icarus 测试运行程序一起使用):

public class Tests
    {
        [Test]
        public void Pass()
        {            
            Assert.IsTrue(true);
        }

        [Test]
        public void Fail()
        {
            Assert.Fail();
        }
    }

当我运行应用程序时,我在results 中得到以下值

这是不正确的,因为确实有要运行的测试!日志文件中包含以下内容

禁用插件“Gallio.VisualStudio.Shell90”:插件启用 条件不满足。请注意,这是预期的 必须托管在第三方内部的插件的行为 应用程序才能工作。启用条件: '${process:DEVENV.EXE_V9.0} 或 ${process:VSTESTHOST.EXE_V9.0} 或 ${process:MSTEST.EXE_V9.0} 或 ${framework:NET35}'。禁用插件 'Gallio.VisualStudio.Tip90':插件依赖于另一个禁用 插件:'Gallio.VisualStudio.Shell90'。

如何解决此问题并找到测试结果?

【问题讨论】:

  • 什么版本的 Visual Studio?
  • 我在VS2012工作
  • 我之前没有与 Gallio 合作过,所以我不确定这是否重要,但您的 Tests 课程不是 [TestFixture]。也许 Gallio 没有看到 [Test] 的读数,因为它找不到固定装置。
  • @Caleb 我已经删除了 TestFixture 属性,它没有任何区别。我还通过 Icarus 测试运行程序确认了 DLL - 它的行为符合预期。

标签: c# unit-testing visual-studio-2012 mbunit gallio


【解决方案1】:

这对我有用,注意我用这个GallioBundle nuget 来获取gallio 和mbunit,所以你安装的可能有些不同。

有关插件的日志消息是预期的,如果您自托管 Gallio 运行时,这些插件将无法工作。

using System;
using System.IO;
using Gallio.Runner;
using Gallio.Runtime;
using Gallio.Runtime.Logging;
using MbUnit.Framework;

public static class Program
{
    public static void Main()
    {
        using (TextWriter tw = new StreamWriter("RunTests.log"))
        {
            var logger = new TextLogger(tw);
            RuntimeBootstrap.Initialize(new RuntimeSetup(), logger);

            TestLauncher launcher = new TestLauncher();
            launcher.AddFilePattern("RunTests.exe");
            TestLauncherResult result = launcher.Run();
            Console.WriteLine(result.ResultSummary);
        }
    }
}

public class Tests
{
    [Test]
    public void Pass()
    {
        Assert.IsTrue(true);
    }

    [Test]
    public void Fail()
    {
        Assert.Fail();
    }
}

这样测试:

› notepad RunTests.cs
› nuget.exe install -excludeversion GallioBundle
  Installing 'GallioBundle 3.4.14.0'.
  Successfully installed 'GallioBundle 3.4.14.0'.
› cd .\GallioBundle\bin
› csc ..\..\RunTests.cs /r:Gallio.dll /r:MbUnit.dll
  Microsoft (R) Visual C# Compiler version 12.0.21005.1
  for C# 5
  Copyright (C) Microsoft Corporation. All rights reserved.    
› .\RunTests.exe
  2 run, 1 passed, 1 failed, 0 inconclusive, 0 skipped

【讨论】:

  • 恐怕不高兴 - 我从他们的网站上获得了 Gallio 和 MBUnit,但我得到“无法解析服务类型 'Gallio.Runner.Projects.ITestProjectManager' 的组件”,因为那里似乎没有为该服务类型注册和启用的任何组件。"
  • 我实际上已经设法以编程方式运行 NUnit 测试,我很乐意使用,所以不要太恐慌。
  • 我已将此标记为答案,因为虽然我无法让它工作,但它与我见过的其他文档相匹配,并且 OP 提供了一个完整的示例
【解决方案2】:

这些是在 Visual Studio 2012 及更高版本中使用简洁的 NUnit 技巧运行 MBUnit 测试的说明。

首先,安装 NUnit 测试适配器扩展(是的,NUnit)

  • 工具 > 扩展和更新 > 在线 > 搜索 NUnit > 安装 NUnit 测试适配器。
  • 您可能需要重新启动 Visual Studio IDE。

然后,您只需向您的测试方法添加一个新的 NUnit 测试属性。在此处查看示例代码(注意顶部的 using 语句)...

//C# example
using MbUnit.Framework;
using NuTest = NUnit.Framework.TestAttribute;

namespace MyTests
{
    [TestFixture]
    public class UnitTest1
    {
        [Test, NuTest]
        public void myTest()
        {
            //this will pass
        }
    }
}

您可以在 Visual Studio 中作为 NUnit 运行和调试测试,而 Gallio Icarus GUI Test Runner 将作为 MBUnit 运行它们(例如,启用并行运行)。您需要通过删除 Gallio 安装位置(即 C:\Program Files\Gallio\bin\NUnit)中的 NUnit 文件夹来阻止 Gallio 运行 NUnit 测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多