NUnit在SetUp中取消测试

时间:2017-05-26 11:56:56

标签: c# nunit xamarin.uitest

我们使用NUnit进行自动化测试,我们有一些必须满足的标准才能运行测试。特别是,我们正在使用Xamarin.UITest进行移动UITesting,我们需要检查我们是否正在测试我们想要测试的平台。不幸的是,我们无法使用类别来实现它。

我们现在的方式是拥有一个我们想要测试的平台列表。在[SetUp]方法中,我们检查当前平台是否包含在该列表中,如果不包含,我们将中止测试。目前,我们通过让Assert.Fail()失败来中止测试。但是,我们宁愿让测试以静默方式退出,没有失败,也没有成功消息,就像它从未运行过一样。这是否可能,如果是这样,怎么办呢?

这是当前的代码:

private IList<Platform> _desiredPlatforms;

public IList<Platform> DesiredPlatforms
{
    get
    {
        if (_desiredPlatforms == null)
        {
            // read from environment variable
        }
        return _desiredPlatforms;
    }
}

[SetUp]
public void BeforeEachTest()
{
   // Check if the current platform is desired
   if (!DesiredPlatforms.Contains(_platform))
   {
        Assert.Fail("Aborting the current test as the current platform " + _platform + " is not listed in the DesiredPlatforms-list");
   }
   _app = AppInitializer.Instance.StartApp(_platform, _appFile);
}

1 个答案:

答案 0 :(得分:3)

听起来像Assert.Inconclusive()Assert.Ignore()更适合您所寻找的内容。

我怎么想象你真的想要做到这一点,但是,它与NUnit的PlatformAttribute相当 - 它将跳过不相关平台上的测试。 NUnit PlatformAttribute尚未针对框架的.NET Standard / PCL版本实现 - 但是,没有理由不能创建自定义属性,为您的特定情况做类似的事情。您可以查看PlatformAttribute的NUnit代码作为示例,并编写自己的等效PlatformHelper,以检测您感兴趣的平台。

编辑:我已经链接到NUnit 3文档,但只是阅读Xamarin.UITest仅限于NUnit 2.x.我相信我所说的一切在NUnit 2中都是等价的 - 你可以在这里找到NUnit 2文档:http://nunit.org/index.php?p=docHome&r=2.6.4