对 Xamarin.iOS 应用进行单元测试

本文档介绍如何为 Xamarin.iOS 项目创建单元测试。 可使用 Touch.Unit 框架对 Xamarin.iOS 执行单元测试,该框架同时包含 iOS 测试运行程序 Touch.Unit 的 NUnit 修改版,可提供一组熟悉的 API 用于编写单元测试。

在 Visual Studio for Mac 中安装测试项目

若要为项目创建单元测试框架,只需向解决方案添加“iOS 单元测试项目”类型的项目。 为此,请右键单击解决方案,然后选择“添加”>“添加新项目”。 从列表中选择“iOS”>“测试”>“统一 API”>“iOS 单元测试项目”(可选择 C# 或 F#)。

Choose either C# or F#

上述操作将创建一个包含基本运行程序,并引用新 MonoTouch.NUnitLite 程序集的基本项目,项目如下所示:

The project in the Solution Explorer

AppDelegate.cs 类包含测试运行程序,如下所示:

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    UIWindow window;
    TouchRunner runner;

    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 ());

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

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

        return true;
    }
}

注意

iOS 单元测试项目类型在 Windows 上的 Visual Studio 2019 或 Visual Studio 2017 中不可用。

编写测试

准备好基本 shell 后,可开始编写第一组测试。

方法是通过创建类进行编写并对测试应用 [TestFixture] 属性。 在每个 TestFixture 类中,应对测试运行程序需调用的每种方法应用 [Test] 属性。 实际测试装置可位于测试项目的任一文件中。

若要快速开始,请选择“添加”/“添加新文件”,然后在 Xamarin.iOS 组中选择“UnitTests”。 这将添加一个主干文件,其中包含三个测试(一个通过、一个失败,一个已忽略),文件如下所示:

using System;
using NUnit.Framework;

namespace Fixtures {

    [TestFixture]
    public class Tests {

        [Test]
        public void Pass ()
        {
                Assert.True (true);
        }

        [Test]
        public void Fail ()
        {
                Assert.False (true);
        }

        [Test]
        [Ignore ("another time")]
        public void Ignore ()
        {
                Assert.True (false);
        }
    }
}

运行测试

若要在解决方案内运行此项目,请右键单击该项目,然后选择“调试项目”或“运行项目”

通过测试运行程序,可查看已注册的测试,并单独选择可执行的测试。

The list of registered testsAn individual text

The run results

从嵌套视图中选择测试装置可运行单个测试装置,或选择“全部运行”运行所有测试。 如果运行默认测试,则应包含三个测试(一个通过、一个失败,一个已忽略)。 报表如下所示,你可以直接向下钻取失败测试并找出有关失败的详细信息:

Screenshot shows a sample report, Test Runner screen.Screenshot shows a sample report, Tests screen.Screenshot shows a sample report, Test screen with test status.

还可在 IDE 中查看“应用程序输出”窗口,以查看正执行的测试及其当前状态。

编写新测试

NUnitLite 是 Touch.Unit 项目的 NUnit 修改版本。 它是一个用于 .NET 的轻量级测试框架,以 NUnit 中的概念为基础,并提供其中的部分功能。 该框架使用的资源极少并可在资源受限的平台(例如嵌入式开发和移动开发所用的平台)上运行。 Xamarin.iOS 中为你提供了 NUnitLite API。 对于此单元测试模板所提供的基本主干,主入口点应为 Assert 类方法。

除 assert 类方法外,还会在 NUnitLite 包含的以下命名空间上拆分单元测试功能: