简体   繁体   English

NUnit取消设置中的测试

[英]NUnit cancel a test in SetUp

We use NUnit for our automated tests and we have some criteria that must be met in order for a test to run. 我们使用NUnit进行自动化测试,并且必须满足一些条件才能运行测试。 In particular, we are using Xamarin.UITest for mobile UITesting and we need to check if we are currently testing a platform that we want to test. 特别是,我们使用Xamarin.UITest进行移动UITesting,我们需要检查当前是否正在测试要测试的平台。 Unfortunately, there is no way for us to do it using categories. 不幸的是,我们无法使用类别来做到这一点。

The way we do it right now is to have a list of platforms we want to test. 现在,我们的方法是列出要测试的平台。 In the [SetUp] method we then check if the current platform is contained in that list and if not, we abort the test. 然后,在[SetUp]方法中,检查当前平台是否包含在该列表中,如果没有,则中止测试。 Currently, we abort the test by letting it fail with Assert.Fail() . 当前,我们通过使用Assert.Fail()使测试失败来中止测试。 However, we would prefer to let the test exit silently, with no failiure and no success message, just as if it was never run. 但是,我们宁愿让测试以静默方式退出,没有失败且没有成功消息,就像从未运行过一样。 Is that even possible and if so, how can it be done? 那有可能吗?如果可以,怎么办?

Here's the current code: 这是当前代码:

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);
}

Sounds like either Assert.Inconclusive() or Assert.Ignore() better fit what you're looking for. 听起来像Assert.Inconclusive()Assert.Ignore()更适合您要寻找的东西。

How I imagine you'd really want to do this however, is with something equivalent to NUnit's PlatformAttribute - which will skip the tests on the irrelevant platforms. 我怎么想像您真正想要执行此操作的原因是与NUnit的PlatformAttribute等效-它会跳过无关平台上的测试。 The NUnit PlatformAttribute isn't implemented for the .NET Standard/PCL versions of the framework yet - however, there's no reason you couldn't make a Custom Attribute, to do a similar thing for your particular case. NUnit PlatformAttribute尚未针对框架的.NET Standard / PCL版本实现-但是,没有理由您不能创建Custom Attribute,以针对您的特殊情况做类似的事情。 You can look at the NUnit code for the PlatformAttribute as an example, and write your own equivalent of PlatformHelper , to detect the platforms you're interested. 您可以查看PlatformAttribute的NUnit代码作为示例,并编写自己的PlatformHelper等效项,以检测您感兴趣的平台。

Edit: I've linked to the NUnit 3 docs, but just read Xamarin.UITest is limited to NUnit 2.x. 编辑:我已经链接到NUnit 3文档,但只是阅读Xamarin.UITest限于NUnit2.x。 I believe everything I've said is equivalent in NUnit 2 - you can find the NUnit 2 docs here: http://nunit.org/index.php?p=docHome&r=2.6.4 我相信我所说的一切在NUnit 2中都是等效的-您可以在此处找到NUnit 2文档: http : //nunit.org/index.php? p=docHome&r= 2.6.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM