如何用Gallio和MBUnit以编程方式运行单元测试?

时间:2014-01-10 12:18:03

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

我正在尝试以编程方式检查我的单元测试是否正在作为部署过程的一部分传递。该应用程序使用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}或$ {process:VSTESTHOST.EXE_V9.0}或   $ {process:MSTEST.EXE_V9.0}或$ {framework:NET35}'。已禁用的插件   'Gallio.VisualStudio.Tip90':该插件取决于另一个禁用   插件:'Gallio.VisualStudio.Shell90'。

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

1 个答案:

答案 0 :(得分: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