Assert.Pass Xunit NUnit MsTest以条件

时间:2018-05-17 11:44:47

标签: c# unit-testing nunit mstest xunit

我正在编写UI测试。这是为了检查我在Web.Config中启用的错误404页面;

<customErrors mode="On" redirect="~/Errors/"/>

这一切都运行正常,但我只将自定义错误设置为&#34; On&#34;而在&#34; UAT&#34;发展环境。如果我在&#34; Dev&#34;或者&#34; IST&#34;然后我仍然希望看到默认的ASP.Net错误。

现在回到使用Selenium的UI测试

    public string GetAlertBoxDetails()
    {
        IWebElement alertBox = _driver.FindElement(By.CssSelector(".alert.alert-danger"));
        return alertBox.Text;
    }

正如您所看到的,我正在检测Bootstrap&#34; .alert.alert-danger&#34;框并将文本返回内部。然后我检查这个文本是否包含&#34;抱歉,该页面不存在。&#34;。我正在使用Specflow作为文本故事。

    [Then(@"The user should be told that no such page exists")]
    public void ThenTheUserShouldBeToldThatNoSuchPageExists()
    {
        string alertboxDetail = GetAlertBoxDetails();
        Assert.IsTrue(alertboxDetail.Contains("Sorry, that page doesn't exist."), "Couldn't find the message \"Sorry, that page doesn't exist.\"");
    }

这一切都很好,然而我只希望这个测试在UAT环境中运行。这是因为元素&#34; .alert.alert-danger&#34;只有在customErrors设置为&#34; Off&#34;时才会找到。为此,我在测试中包含了这一步。

    [Given(@"I am in the UAT environment")]
    public void GivenIAmInTheUATEnvironment()
    {
        var env = EnvironmentType;
        if (env != EnvironmentType.Uat)
        {
            Assert.Inconclusive($"Cannot run this test on environment: {env}. " +
                $"This test is only for the UAT environment.");
        }
        else
        {
            Assert.IsTrue(true);
        }
    }

这再次正常。我唯一的问题是我不想使用&#34; Assert.Inconclusive&#34;我宁愿&#34; Assert.Pass&#34;如果在非UAT环境中进行测试,则表示测试通过。

我看到XUnit有一个Assert.Pass函数,但这可以在MsTest中完成吗?强制测试通过而不继续下一个断言。在specflow中,我正在运行&#34;给出&#34;我想阻止它继续进行&#34;然后&#34;步骤

1 个答案:

答案 0 :(得分:1)

WRT NUnit,您可以尝试Assert.Pass。因为我旅行,我现在无法自己尝试。我的不确定性是,我不确定如果你在SetUp中执行它会阻止测试运行,这就是Given映射到的。

我的观点是接受你所寻找的行为,所有代码都属于测试本身而不是Given。通常做的是实际创造你期望的情况,即改变环境。这显然不可能在这里,所以我只是简单地将环境检查放在测试本身。我甚至不会使用Assert.Pass,除非你想要一个特殊的消息,如果环境错误,我就跳过测试代码。作为附带好处,这种方法适用于所有三个测试框架。

虽然你没有问,但我必须说你给我的指示显示测试即使没有运行就过去了,对我来说似乎很疯狂!