2016-05-23 34 views
0

我必須要說的第一件事是,我不太確定如何解釋這個問題,因爲我是Xamarin的新手。Xamarin NUnitLite測試方法:如何在每個平臺上運行共享測試?

我在Xamaring中構建了一個應用程序,其目標是成爲跨平臺。

這些步驟如下:

  1. 創建解決方案
  2. 新建項目,名稱:Demo.UI.TestHarness.iOS,類型:iOS的統一單元測試應用
  3. 新建項目,名稱:Demo.UnitTests,類型:跨平臺移植圖書館
  4. Demo.UI.TestHarness.iOS啓動項目
  5. 添加NuGet包NUnitLiteDemo.UnitTests
  6. 添加引用到Demo.UnitTestsDemo.UI.TestHarness.iOS

這樣做,我創建DummyTest一類Demo.UnitTests

using System; 
using NUnit.Framework; 

namespace Demo.UnitTests 
{ 
    [TestFixture] 
    public class DummyTest 
    { 
     [Test] 
     public void DUMMY() 
     { 
      Assert.True (false); 
     } 
    } 
} 

我添加到文件UnitTestAppDelegateDemo.UI.TestHarness.iOSDummyTest參考:

using System; 
using System.Linq; 
using System.Collections.Generic; 

using Foundation; 
using UIKit; 
using MonoTouch.NUnit.UI; 
using Demo.UnitTests; 

namespace Demo.UI.TestHarness.iOS 
{ 
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS. 
    [Register ("UnitTestAppDelegate")] 
    public partial class UnitTestAppDelegate : UIApplicationDelegate 
    { 
     // class-level declarations 
     UIWindow window; 
     TouchRunner runner; 

     // 
     // This method is invoked when the application has loaded and is ready to run. In this 
     // method you should instantiate the window, load the UI into it and then make the window 
     // visible. 
     // 
     // You have 17 seconds to return from this method, or iOS will terminate your application. 
     // 
     public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
     { 
      // create a new window instance based on the screen size 
      window = new UIWindow (UIScreen.MainScreen.Bounds); 
      runner = new TouchRunner (window); 

      // register every tests included in the main application/assembly 
      // runner.Add (System.Reflection.Assembly.GetExecutingAssembly()); 
      runner.Add(typeof(DummyTest).Assembly); 

      window.RootViewController = new UINavigationController (runner.GetViewController()); 

      // make the window visible 
      window.MakeKeyAndVisible(); 

      return true; 
     } 
    } 
} 

現在,我可以構建項目並運行調試器仿真,但沒有ests出現。

相反,如果我直接添加DummyTestDemo.UI.TestHarness.iOS項目中,完全忘記了Demo.UnitTests項目,它運行按預期(但這不是我想要的,因爲我希望讓測試一起到以後使用相同的測試適用於Android和Mac,無需爲每個平臺重做)。

+0

我通常將我的測試拆分爲便攜式單元測試(聽起來像你的'Demo.UnitTests'),它沒有平臺依賴性。我使用NUnit庫運行這些單元測試(僅在我的開發機器上,而不是在設備上)。對於任何依賴於設備的東西,我直接在iOS(或其他平臺)單元測試應用程序中編寫單元測試,因爲我無法共享依賴於特定平臺的測試。這樣我的共享測試可以非常快速和輕鬆地運行。這只是我設置測試的一種方式。 – dylansturg

回答

1

櫃面任何人想知道爲什麼這不工作 - 這裏是我的研究使我: https://forums.xamarin.com/discussion/16909/cant-test-pcl-test-assembly-from-xamarin-android-nunitlite-or-monotouch-nunit-ui

具體崗位由「勒布巴塞洛繆」。在他的回覆中,他發佈了一項工作(我還沒有嘗試過)。基本問題是:

Xamarin.iOS使用MonoTouch.NUnitLite,Xamarin.Android使用Xamarin.Android.NUnitLite和PCL使用基本NUnit nuget包。

儘管程序集具有相同的名稱並且屬於相同的名稱空間,但它們是完整的不同類型,因此Monotouch測試運行器不會檢測外部[Test]屬性。

+0

我切換到XUnit做測試。他們沒有很好地集成在Xamarin Studio中,但在不同的平臺上工作良好。 – jbssm