如何使用Gallio和MBUnit编程运行单元测试?

7
我将尝试在部署过程中编写程序来检查我的单元测试是否通过。该应用程序使用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 中出现了以下数值:

enter image description here

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

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

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

Visual Studio 的哪个版本? - jessehouwing
我正在使用VS2012工作。 - Liath
1
我之前没有使用过Gallio,所以我不确定这是否有影响,但是你的“Tests”类不是一个“[TestFixture]”。也许Gallio没有看到“[Test]”,因为它找不到fixture。 - Caleb
@Caleb 我已经移除了 TestFixture 属性,但没有任何改变。我还确认了 Icarus 测试运行器中的 DLL - 它的行为符合预期。 - Liath
2个回答

4
这对我很有用,注意我使用了这个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'的组件,因为似乎没有为该服务类型注册和启用任何组件”的错误信息。 - Liath
我已经成功地通过编程方式运行了 NUnit 测试,我很高兴使用它们,所以不要太担心。 - Liath
我已将此标记为答案,因为尽管我无法使其工作,但它与我看到的其他文档匹配,并且原帖提供了一个完整的示例。 - Liath

0

这是在Visual Studio 2012及以上版本中使用巧妙的NUnit技巧运行MBUnit测试的指南。

首先,安装NUnit Test Adapter扩展(是的,NUnit)

  • 工具 > 扩展和更新 > 在线 > 搜索NUnit > 安装 NUnit Test Adapter。
  • 您可能需要重新启动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测试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接