Assert.Pass Xunit NUnit MsTest 在条件下转义

Assert.Pass Xunit NUnit MsTest escape on condition

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

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

一切正常,但是在 "UAT" 开发环境中,我只将自定义错误设置为 "On"。如果我在 "Dev" 或 "IST" 中,那么我仍然希望看到默认的 ASP.Net 错误。

现在回到 UI 使用 Selenium 的测试

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

如您所见,我正在检测 Bootstrap“.alert.alert-danger”框并返回里面的文本。然后我检查此文本是否包含 "Sorry, that page doesn't exist."。我正在为文本故事使用 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 环境中对 运行 进行此测试。这是因为元素“.alert.alert-danger”只有在 customErrors 设置为 "Off" 时才会被发现。为此,我在测试中包含了这一步。

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

这同样可以正常工作。我唯一的问题是我不想使用 "Assert.Inconclusive" 我宁愿 "Assert.Pass" 并说如果在非 UAT 环境中执行测试通过。

我看到 XUnit 有一个 Assert.Pass 函数,但这可以在 MsTest 中完成吗?强制测试通过而不继续下一个断言。在 specflow 中,我正在 运行 宁 "given" 步骤我想阻止它继续 "Then" 步骤。

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

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

虽然你没有问,但我不得不说,你得到的指示显示测试通过,即使它没有通过 运行 对我来说似乎很疯狂!