Xamarin NUnitLite测试方法:如何在每个平台上运行共享测试?

问题描述:

我必须要说的第一件事是,我不太确定如何解释这个问题,因为我是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

柜面任何人想知道为什么这不工作 - 这里是我的研究使我: 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