首页 > 解决方案 > 如何仅在特定于 Android 和 iOS 的 AppCenter 上运行自动化测试?

问题描述

我正在使用 Xamarin.UITest 和 NUnit 来自动化移动应用程序,并正在使用 Azure DevOps 管道在 AppCenter 上运行测试。这些测试目前在 AppCenter 上的 Android 和 iOS 平台上运行良好。但是,我有一个要求,我想只在 Android 上运行特定的测试,而只在 iOS 上运行。任何人都可以分享一些关于如何实现这一点的建议吗?

标签: visual-studio-app-centerxamarin.uitest

解决方案


最后,我自己找到了解决方案。我通过在 Setup 方法中的每次测试之前检查平台来使其工作。请找到我用 C# 和 NUnit 编写的以下代码。例如,有 2 个测试,其中只有一个测试应该运行,另一个不应该运行,然后有一个测试(需要运行)的类别标记为 iOS,而另一个没有类别。

[TextFixture(Platform.Android)]
[TextFixture(Platform.iOS)]
public class Sample{
       [SetUp]
        public void RunBeforeEachTest()
        {
            if(AppManager.Platform == Platform.iOS)
            {
                if(!hasiOSCategory())
                {
                    Assert.Inconclusive();
                }
            }
        }

然后在 setup 方法中进行检查,验证当前平台是否为 iOS 并检查测试是否有 iOS 类别

// this method is used to get all categories of the test.
        private static bool hasiOSCategory()
        {   
            var categories = TestContext.CurrentContext.Test?.Properties["Category"];
            bool hasTestContainsiOSCategory = categories != null && categories.Contains(Constant.iOS);
            return hasTestContainsiOSCategory;
        }

        [Test,Category("iOS")]
        public void TestWithCategory()
        {
            \\your code
        }

        [Test]
        public void TestWithoutCategory()
        {
        }
}

最后,如果可以在框架中实现一件事,那么它也应该在 AppCenter 上工作。


推荐阅读