zoukankan      html  css  js  c++  java
  • Xamarin+Prism开发详解八:自动化测试之NUnit实践

    自动化测试很重要!很重要!以前多是手动测试,没有写过测试用例。这样的结果就是发现bug改了之后关联的其他功能又要从新测一遍。这样既浪费时间与成本,而且很无聊。之所以选择NUnit是公司需要,现在.net 系都流行xunit,有空再看看它吧。Nunit的xamarin应用有很多欠缺的地方,特为大伙补补坑。Nunit本身的Test,SetUp,Category等属性的使用下次再说。

    NUnit简介

    ... an excellent example of idiomatic design. Most folks who port xUnit just transliterate the Smalltalk or Java version. That's what we did with NUnit at first, too. This new version is NUnit as it would have been done had it been done in C# to begin with.

    NUnit是一个单体测试框架,支持.net系所有语言,它的祖先是Java的JUnit。目前最新为3.6版本,已经重写了很多特性,目前支持.net core,xamarin,uwp等很多.net 平台。

    Xamarin应用

    Xamarin项目测试分类:

    • 平台无关部分(业务逻辑等):创建Nunit测试类库添加参照就可以了,不用启动实机或者模拟器测试。(本文略过,会第二种这种肯定也会)

    image

    • 平台相关部分(文件操作,数据库操作等):如果多个平台的测试用例一样,可以创建共享项目(Share Project)放测试用例,需要启动实机或模拟器测试。(重点介绍)

    添加Nunit测试项目的方法有两种:

    1. 通过Nunit的模板创建测试项目。
    2. 创建相应的项目然后再添加Nunit相关的Nuget包(Nunit与Nunit.Xamarin)。

    第一种方法简单但是问题特多,所以特拿它来做说明。第二种参照第一种基本上可以自行搞定略过。

    1,安装NUnit Templates for Visual Studio模板

    模板默认安装如下内容:

    Project Templates

    TemplatePlatformLanguage
    NUnit 3 Unit Test Project Desktop C#
    NUnit 3 Unit Test Project Desktop Visual Basic
    NUnit 3 Unit Test Project Xamarin Android1 C#
    NUnit 3 Unit Test Project Xamarin iOS1 C#
    NUnit 3 Unit Test Project Xamarin UWP1,2 C#
    NUnit 3 Unit Test Project Xamarin WP8.11 C#

    Item Templates

    TemplateLanguageDescription
    NUnit Test Fixture C# An NUnit unit test class
    NUnit Test Fixture Visual Basic An NUnit unit test class
    NUnit SetUp Fixture C# Code that runs before and after all the tests in the assembly
    NUnit SetUp Fixture Visual Basic Code that runs before and after all the tests in the assembly

    Code Snippets

    SnippetShortcutLanguage
    Test Fixture ntestfixture C#
    Test Method ntest C#
    Test Case ntestcase C#

    下载地址:https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTemplatesforVisualStudio

    或者

    通过Visual Studio的扩展更新添加【NUnit Templates for Visual Studio

    image

    安装成功后在Cross-Platform分类下面就会有Test的模板了:

    image

    2,新建iOS测试项目

    image

    成功后的项目如下:

    image

    以上圈起来的为问题点:(第一个坑)

    • 【AppDelegate.cs.txt】是多余的可以删除。(如果手动添加Nunit.xamarin包也会生成此项,这个时候需要用它替换原来的AppDelegate.cs文件)
    • 【Resources】文件夹下的文件是损坏的无效文件,需要自行替换。

    image

    不信的可以先编译试试,绝对会出现如下错误:

    image

    替换文件后再编译肯定可以成功:

    image

    image

    由于默认自带了一个测试用例所以可以启动看看效果:

    image

    image

    image

    • Run Tests:再次启动测试
    • All Results:查看全部测试结果
    • Failed Results:查看失败的测试结果

    效果看上去还是很不错的!

    AppDelegate.cs里头包含Nunit测试引擎的启动设置,以及外部测试程序集的设置。如果是Share project参照的话这里就不用修改,这也是为什么推荐使用Share project的原因。

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
            {
                global::Xamarin.Forms.Forms.Init();
    
                // This will load all tests within the current project
                var nunit = new NUnit.Runner.App();
    
                // If you want to add tests in another assembly
                //nunit.AddTestAssembly(typeof(MyTests).Assembly);
    
                // Do you want to automatically run tests when the app starts?
                nunit.AutoRun = true;
    
                LoadApplication(nunit);
    
                return base.FinishedLaunching(app, options);
            }

    自动带的测试用例,Nunit具体的这些属性以及方法如何使用可以参照官网,有空我再补补吧。

    [TestFixture]
        [Category(nameof(TestClass))]
        public class TestClass
        {
            [Test]
            public void TestMethod()
            {
                // TODO: Add your test code here
                Assert.Pass("Your first passing test");
            }
        }

    3,新建Android测试项目

    image

    生成成功后的效果:(公司的电脑每次都无法创建成功哭泣的脸,多半是Android SDK与Xamarin版本的问题。)

    image

    以上圈起来的为问题点:(第二个坑)

    • 【MainActivity.cs.txt】是多余的可以删除。(如果手动添加Nunit.xamarin包也会生成此项,这个时候需要用它替换原来的MainActivity.cs文件)
    • 【Resources】文件夹下的icon.png文件是损坏的无效文件,需要自行替换。

    image

    直接编译的话应该会有如下等错误:(由于编译器默认使用了最新版的Android SDK

    image

    解决办法:安装最新版的xamarin.Forms.

    image

    记得替换Resources下面所有文件夹里面的icon.png,不然会有如下错误:

    image

    替换之后编译就没问题了:

    image

    image

    启动之后的效果和iOS类似如下:

    image

    image

    image

    MainActivity.cs里头包含Nunit测试引擎的启动设置,以及外部测试程序集的设置。如果是Share project参照的话这里就不用修改,这也是为什么推荐使用Share project的原因。

    protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
    
                // This will load all tests within the current project
                var nunit = new NUnit.Runner.App();
    
                // If you want to add tests in another assembly
                //nunit.AddTestAssembly(typeof(MyTests).Assembly);
    
                // Do you want to automatically run tests when the app starts?
                nunit.AutoRun = true;
    
                LoadApplication(nunit);
            }

    由于我已经把测试用例放到了Share Project,代码和上面iOS的一样。

    4,新建UWP测试项目

    image

    设置版本

    image

    然后就出错了:(如果你的电脑安装了10240的SDK的话没问题

    image

    第三坑:明明最低选择了10586,怎么还必须的有10240的版本?实际上确又创建了此项目:

    image

    无法添加到工程的罪魂祸首在这,这VS模板不知道是那位大哥建的啊!怎么就固定死了10240???

    image

    解决办法也就有了,把替换成你想要的版本就可以了:

    image

    添加到工程后的情况:

    image

    以上圈起来的为问题点:(第四个坑)

    • 【Project.json】居然没有Xamarin.Forms,Nunit,Nunit.Xamarin包的设置。
    • 【Assets】文件夹下的文件是损坏的无效文件,需要自行替换。

    image

    image

    解决办法:

    先替换图片

    image

    接着添加以下包:

    image

    编译应该就没问题了:

    image

    添加共享项目参照,启动试试效果:

    image

    image

    image

    MainPage.cs里头包含Nunit测试引擎的启动设置,以及外部测试程序集的设置。如果是Share project参照的话这里就不用修改,这也是为什么推荐使用Share project的原因。

    public sealed partial class MainPage
        {
            public MainPage()
            {
                InitializeComponent();
    
                // Windows Universal will not load all tests within the current project,
                // you must do it explicitly below
                var nunit = new NUnit.Runner.App();
    
                // If you want to add tests in another assembly, add a reference and
                // duplicate the following line with a type from the referenced assembly
                nunit.AddTestAssembly(typeof(MainPage).GetTypeInfo().Assembly);
    
                // Do you want to automatically run tests when the app starts?
                nunit.AutoRun = true;
    
                LoadApplication(nunit);
            }
        }

    5,新建Nunit测试类库

    在【测试】分类里面可以找到如下模板:

    image

    为了能在测试资源管理器里面看到测试条目,添加【NUnit3TestAdapter】Nuget包:

    image

    编译测试项目之后测试资源管理器就会有条目:

    image

    点击全部运行就可以跑相关测试:

    image

    最后整体的测试代码结构如下:

    image

    代码地址:https://github.com/NewBLife/XamarinDemo/tree/master/Demo.Tests

    总结

    测试框架本身还是不错的,测试速度与测试结果展示等方面都还不错。至于它的xamarin模板确实太不靠谱,建议直接创建普通的项目然后添加Nunit相关包与相应的代码,这样也许问题还会少些。希望已经帮你们填完坑了。

  • 相关阅读:
    一些好用的小工具
    App随机测试之Monkey和Maxim
    Appium如何自动判断浏览器驱动
    最简单的一个Appium测试Android Web APP的代码demo
    pytest使用allure生成测试报告的2种命令
    使用order by in()将快到期的数据排到最上方。
    关于jQuery click()方法重复提交的问题
    关于List removeAll失效的问题
    根据年和月计算对应的天数
    jquery通过监听输入框实现值的自动计算
  • 原文地址:https://www.cnblogs.com/lixiaobin/p/NunitForXamarinForms.html
Copyright © 2011-2022 走看看