我必須要說的第一件事是,我不太確定如何解釋這個問題,因爲我是Xamarin的新手。Xamarin NUnitLite測試方法:如何在每個平臺上運行共享測試?
我在Xamaring中構建了一個應用程序,其目標是成爲跨平臺。
這些步驟如下:
- 創建解決方案
- 新建項目,名稱:
Demo.UI.TestHarness.iOS
,類型:iOS的統一單元測試應用 - 新建項目,名稱:
Demo.UnitTests
,類型:跨平臺移植圖書館 - 讓
Demo.UI.TestHarness.iOS
啓動項目 - 添加NuGet包
NUnitLite
到Demo.UnitTests
- 添加引用到
Demo.UnitTests
在Demo.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);
}
}
}
我添加到文件UnitTestAppDelegate
在Demo.UI.TestHarness.iOS
這DummyTest
參考:
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出現。
相反,如果我直接添加DummyTest
我Demo.UI.TestHarness.iOS
項目中,完全忘記了Demo.UnitTests
項目,它運行按預期(但這不是我想要的,因爲我希望讓測試一起到以後使用相同的測試適用於Android和Mac,無需爲每個平臺重做)。
我通常將我的測試拆分爲便攜式單元測試(聽起來像你的'Demo.UnitTests'),它沒有平臺依賴性。我使用NUnit庫運行這些單元測試(僅在我的開發機器上,而不是在設備上)。對於任何依賴於設備的東西,我直接在iOS(或其他平臺)單元測試應用程序中編寫單元測試,因爲我無法共享依賴於特定平臺的測試。這樣我的共享測試可以非常快速和輕鬆地運行。這只是我設置測試的一種方式。 – dylansturg