简体   繁体   中英

Xamarin - Unit Test App (android) - defining usage

Today I have been looking at Unit Test App (Android) for our Xamarin Android app.

在此处输入图片说明

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 . For our PCL/Share Class library that contains some business logic I use just a standard NUnit project.

Is the "Unit Test App (Android) project meant to be used as:

  1. If I have an "Android Class Library", reference it there and test code that is contained there?
  2. Reference an Android project and then with some magic, test the code that is contained there? (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. 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:

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.

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. 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.

Again, quoting that thread, one of the possible solutions is

MyProj (Solution)

  • MyProj (PCL)
  • MyProj.Android (default Xamarin project)
  • MyProj.Android.Shared (Android Class Library)
  • MyProj.Android.Test> (Android Unit Test App)
  • MyProj.iOS (default Xamarin project)

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

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. 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.

Here's a good blog post on the subject: https://medium.com/@_kbremner/automating-android-unit-tests-with-xamarin-6058d101eb97#.5t77lbvzd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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