簡體   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