繁体   English   中英

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

[英]How to programmatically run unit tests with Gallio and MBUnit?

我正在尝试以编程方式检查我的单元测试是否正在作为部署过程的一部分传递。 该应用程序使用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'。

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

这对我有用 ,注意我使用这个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

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

首先,安装NUnit Test Adapter扩展(是的,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安装位置中的NUnit文件夹来阻止Gallio运行NUnit测试,即C:\\ Program Files \\ Gallio \\ bin \\ NUnit

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM