简体   繁体   English

Xamarin-单元测试应用程序(android)-定义用法

[英]Xamarin - Unit Test App (android) - defining usage

Today I have been looking at Unit Test App (Android) for our Xamarin Android app. 今天,我一直在为Xamarin Android应用查看单元测试应用(Android)

在此处输入图片说明

However lack of any official documentation and proper explanation left me very inconclusive of how to actually use this => what to test with it ? 但是,由于缺少任何官方文档和适当的解释,我对如何实际使用此=> 进行测试尚无定论。

I understand that for UI Tests we have the UI Test App . 我了解,对于UI测试,我们具有UI测试应用程序 For our PCL/Share Class library that contains some business logic I use just a standard NUnit project. 对于包含某些业务逻辑的PCL / Share类库,我仅使用一个标准的NUnit项目。

Is the "Unit Test App (Android) project meant to be used as: “ Unit Test App(Android)项目是否打算用作:

  1. If I have an "Android Class Library", reference it there and test code that is contained there? 如果我有一个“ Android类库”,请在那里引用并测试其中包含的代码?
  2. Reference an Android project and then with some magic, test the code that is contained there? 引用一个Android项目,然后用一些魔法测试其中包含的代码? (although I can't really imagine how that would work) (尽管我真的无法想象这将如何工作)
  3. Something else that I haven't thought of. 我没想到的其他事情。

This is an example of some tests in the Unit Test App (Android) project. 这是单元测试应用程序(Android)项目中一些测试的示例。 I hope you can agree that this is a very inconclusive example: 我希望您可以同意这是一个非常不确定的示例:

   [TestFixture]
    public class TestsSample
    {

        [SetUp]
        public void Setup() { }


        [TearDown]
        public void Tear() { }

        [Test]
        public void Pass()
        {
            Console.WriteLine("test1");
            Assert.True(true);
        }

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

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

        [Test]
        public void Inconclusive()
        {
            Assert.Inconclusive("Inconclusive");
        }
    }

I want to quote JohnathanPryor from Xamarin to answer your question: 我想引用Xamarin的JohnathanPryor回答您的问题:

Don't add Application projects to Application projects; 不要将应用程序项目添加到应用程序项目中; things generally won't work as you expect. 通常情况下,您将无法正常工作。

Instead... 代替...

Because adding Application projects as project references to Application projects is (still!) not advised (at least until using Wear project support), this was never a good way to unit test your code. 因为不建议(至少在使用Wear项目支持之前)将Application项目作为对Application项目的项目引用添加(至少!),所以这从来不是对单元代码进行单元测试的好方法。

Thus, what you should instead do is place any code you want to have used by a Unit Test Project be within an Android Library (or PCL) project. 因此,您应该做的是将要由单元测试项目使用的任何代码放在Android库(或PCL)项目中。 This will ensure that the type you're testing can use Android Resources "normally", have things generally work as expected and desired, and your code will be usable by both the Unit Test project and the Application project you really care about. 这将确保您正在测试的类型可以“正常”使用Android资源,使一切正常地按预期和期望运行,并且您真正关心的单元测试项目和Application项目都可以使用您的代码。

Again, quoting that thread, one of the possible solutions is 再次引用该线程,可能的解决方案之一是

MyProj (Solution) MyProj(解决方案)

  • MyProj (PCL) MyProj(PCL)
  • MyProj.Android (default Xamarin project) MyProj.Android(默认Xamarin项目)
  • MyProj.Android.Shared (Android Class Library) MyProj.Android.Shared(Android类库)
  • MyProj.Android.Test> (Android Unit Test App) MyProj.Android.Test>(Android单元测试应用程序)
  • MyProj.iOS (default Xamarin project) MyProj.iOS(默认Xamarin项目)

Where MyProj.Android contains almost nothing and MyProj.Android.Shared contains everything (Activities/Bundles et all) so that they can be referenced in the Android unit tests 其中MyProj.Android几乎不包含任何内容,而MyProj.Android.Shared包含所有内容(活动/捆绑等),因此可以在Android单元测试中引用它们

or 要么

using a Shared Project 使用共享项目

Basically the Android Unit Test project has it's own UI and allows you to run your Unit Tests while executing on the actual platform, ie on an actual Android device or emulator. 基本上,Android单元测试项目具有其自己的UI,并允许您在实际平台(即,实际的Android设备或模拟器)上执行时运行单元测试。 The UI is very simple and juts allows for starting the unit tests and seeing the results. 用户界面非常简单,突出显示可以启动单元测试并查看结果。 And yes you can reference an Android class library and I would imagine an Android app project as well. 是的,您可以引用一个Android类库,我也可以想象一个Android应用程序项目。

Here's a good blog post on the subject: https://medium.com/@_kbremner/automating-android-unit-tests-with-xamarin-6058d101eb97#.5t77lbvzd 这是一篇关于该主题的好博客文章: https : //medium.com/@_kbremner/automating-android-unit-tests-with-xamarin-6058d101eb97#.5t77lbvzd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM