我们使用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);
}发布于 2017-05-26 12:21:15
听起来Assert.Inconclusive()或Assert.Ignore()更适合你想要的东西。
然而,我认为您真正想要这样做的方法是使用与NUnit的PlatformAttribute相当的东西--它将跳过无关平台上的测试。NUnit PlatformAttribute还没有为框架的.NET标准/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
https://stackoverflow.com/questions/44201034
复制相似问题