使用分类测试

通过从 NUnit 框架添加 CategoryAttribute ,可以将 uitest 分组为不同的类别。 这些类别为运行的测试提供了一些灵活性。

例如,应用程序可能有一组专用于平板电脑的测试,另一组测试用于手机。 通过类别,可以独立于手机测试运行特定于平板电脑的测试。

另一种常见方案是将慢速测试与快速测试隔离开来。 快速测试将更频繁地运行,可能是每次提交到源代码控制。 较慢的测试运行频率较低,例如一天一次。

通过将添加 CategoryAttribute 到类或方法,可以按测试装置或测试类别对 uitest 进行分类。 可以分配多个类别。 下面的类显示分类的示例:

[TestFixture]
[Category("nerp")]
public class Tests
{
    iOSApp app;

    [SetUp]
    public void BeforeEachTest()
    {
        app = ConfigureApp.iOS.StartApp();
    }

    [Test]
    [Category("derp")]
    [Category("erp")]
    public void CreditCardNumber_TooShort_DisplayErrorMessage()
    {
        app.WaitForElement(c=>c.Class("UINavigationBar").Marked("Simple Credit Card Validator"));
        app.EnterText(c=>c.Class("UITextField"), new string('9', 15));
        app.Tap(c=>c.Marked("Validate Credit Card").Class("UIButton"));
        app.WaitForElement(c => c.Marked("Credit card number is too short.").Class("UILabel"));
    }


    [Test]
    [Category("flerp")]
    public void CreditCardNumber_TooLong_DisplayErrorMessage()
    {
        app.WaitForElement(c=>c.Class("UINavigationBar").Marked("Simple Credit Card Validator"));
        app.EnterText(c=>c.Class("UITextField"), new string('9', 17));
        app.Tap(c=>c.Marked("Validate Credit Card").Class("UIButton"));
        app.WaitForElement(c => c.Marked("Credit card number is too long.").Class("UILabel"));
    }

}

可以通过两种方式根据类别运行 Uitest:

  • appcenter -App Center 的 命令行界面
  • nunit-console.exe -用于 NUnit 测试的命令行运行程序。 它还用于在本地运行 Uitest。

备注

App Center 不服从 ExplicitAttribute ; 标记为的测试 Explicit 仍将运行。

按类别本地运行测试

在本地运行测试是使用 nunit-console.exe NUnit 的命令行运行程序完成的。 命令行开关 --where 使用测试选择语言 来 匹配类别

nunit-console .\CreditCardValidator.iOS.UITests\bin\Debug\CreditCardValidator.iOS.UITests.dll --where="cat == flerp"

按类别向App Center测试提交测试

警告

️ 不能指定包含空格的 NUnit 类别名称进行上传。

可以指示App Center测试使用 参数运行测试 --include-category 的子集。

appcenter test run uitest --app "<APP NAME>" --devices <DEVICE SET ID> --app-path <PATH TO IPA> --test-series "<TEST SERIES>" --locale "en_US" --build-dir <PATH TO UITEST BUILD DIRECTORY> 
--include-category flerp 

还可使用 参数按类别排除某些 --exclude-category 测试。

appcenter test run uitest --app "<APP NAME>" --devices <DEVICE SET ID> --app-path <PATH TO IPA> --test-series "<TEST SERIES>" --locale "en_US" --build-dir <PATH TO UITEST BUILD DIRECTORY> 
--exclude-category erp

有关详细信息,请参阅 NUnit 控制台指南 ,了解如何使用类别 表达式 根据类别组合包括或排除测试。

使用 --fixture-chunk 时,将执行整个测试套件,并忽略 和 --include-cateogry --exclude-category 开关。

备注

️ 在本地运行测试时, IApp 必须使用应用程序路径和应用程序捆绑包进行配置。