【问题标题】:Can not run Unitests with Xamarin无法使用 Xamarin 运行 Unitest
【发布时间】:2019-10-28 13:12:25
【问题描述】:

我想通过单元测试检查我的 Xamarin 项目代码(Cookbook)。我从 Visual Studio (UITest1) 创建了一个 Unitest for Xamarin 项目。当我尝试运行它时,链接器会写入以下错误:

Error   NU1201  Project Cookbook is not compatible with net461 (.NETFramework,Version=v4.6.1) / win-x64. Project Cookbook supports: monoandroid81 (MonoAndroid,Version=v8.1)    UITest1 

我做错了什么?尝试使用 Google,但没有成功。

如果有帮助,这是 Uintests 代码:

using System;
using System.IO;
using System.Linq;
using Cookbook;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Queries;

namespace UITest1
{
    [TestFixture(Platform.Android)]
    [TestFixture(Platform.iOS)]
    public class Tests
    {
        IApp app;
        Platform platform;
        private Ingredient ingr;

        public Tests(Platform platform)
        {
            this.platform = platform;
        }

        [SetUp]
        public void BeforeEachTest()
        {
            //app = AppInitializer.StartApp(platform);

            ingr = new Ingredient();
        }

        [Test]
        public void WelcomeTextIsDisplayed()
        {
            AppResult[] results = app.WaitForElement(c => c.Marked("Welcome to Xamarin.Forms!"));
            app.Screenshot("Welcome screen.");

            Assert.IsTrue(results.Any());
        }


         [Test]
        public void ParseFromString()
        {
            Ingredient ingr = new Ingredient();
            ingr.TryToParseFromString("Ingredients");
            Assert.AreEqual(0, ingr.Amount, "amount problem");
            Assert.AreEqual(null, ingr.Item, "item problem");
            Assert.AreEqual(null, ingr.Units, "units problem");
            Assert.AreEqual("Ingredients", ingr.Unparsed, "unparsed problem");
        }

【问题讨论】:

  • 你尝试过使用 NUnit 吗?
  • 是的,我正在使用 UNit
  • 您是否调查过您遇到的错误?兼容性可能表明cookbook仅与android代码兼容。
  • 我确实查看了谷歌中的错误,但没有发现任何帮助。 “只兼容安卓代码”是什么意思?
  • 您运行的是什么版本的 Visual Studio?也许这个链接可能会帮助nopcommerce.com/boards/t/51749/…

标签: unit-testing xamarin xamarin.forms


【解决方案1】:

我发现您混淆了单元测试和 UI 测试的概念,因为您在测试项目中同时拥有这两者。您应该做的是创建两个单独的项目,例如Cookbook.UITestsCookbook.UnitTests。原因是 UI 测试旨在模拟用户行为,同时在模拟器、真实设备或云测试服务上运行。另一方面,单元测试应该测试代码应用程序的业务逻辑之类的东西(简而言之)。

我建议您执行以下操作:

  1. 创建两个单独的项目Cookbook.UITestsCookbook.UnitTests
  2. 关注great guidance by SushiHangover,了解如何设置单元测试项目。
  3. 按照 Microsoft 的 official documentation 设置 UITest 项目。

【讨论】: