NUnit Framework AssertionException 没有明确的原因

NUnit Framework AssertionException with no clear reason why

根据我下面的代码。我正在测试 UI 以确保应用程序的正确权限应用于某些用户帐户。

当我在下面 运行 时,测试在“Assert.Pass(); // 标记为通过这里 ok” 处通过。 在此之后,代码进入 catch 块。

异常消息完全是空白,堆栈跟踪是:

at NUnit.Framework.Assert.Pass(String message, Object[] args) at NUnit.Framework.Assert.Pass() at SigangeCoreUiTests.Tests.PermissionTests.BslUserPermissionTest() in C:...\PermissionTests.cs:line 90

由于断言失败,测试显然失败了,但是没有理由引发异常并且它已经通过了。截屏没有引起问题,似乎是 Assert.Pass.

为什么我的代码会这样做?

 public void BslUserPermissionTest()
    {
        var _testUsername = _configuration.GetValue<string>("PermissionsTestUserList:BSLUser:Username");
        var _testPassword = _configuration.GetValue<string>("PermissionsTestUserList:BSLUser:Password");
        var wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 60));

        try
        {
            if (!LoginHandler(_testUsername, _testPassword))
            {
                Assert.Fail();
            }

            System.Threading.Thread.Sleep(2000);

            var menuItem = wait.Until(ExpectedConditions.ElementIsVisible(By.LinkText("BSL Interpreter")));
            menuItem.Click();

            var result = wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("app-bsl-interpreter")));

            if (result.Text.Contains("Message for Interpretation"))
            {
                //test no other menu items are present - only expecting the one menu item
                if (ValidateSingleMenuItemPresent())
                {
                    new SupportClasses.ScreenShot().Take(_driver, false);
                    Assert.Fail();
                }

                new SupportClasses.ScreenShot().Take(_driver, true);
                Assert.Pass(); //marks as a pass here ok
            }
            else
            {
                new SupportClasses.ScreenShot().Take(_driver, false);
                Assert.Fail();
            }
        }
        catch (Exception ex) //raises exception for unknown reason
        {
           new SupportClasses.ScreenShot().Take(_driver, false);
           Assert.Fail(); 
        }
        finally
        {
            _driver = new SupportClasses.SsdAuthentiation(_driver).Logout();
        }
    }

Assert.Pass 抛出异常,您应该根据您的测试用例使用 Assert 检查值。

请参阅 GitHub 中的以下代码。

    /// <summary>

    /// Throws a <see cref="SuccessException"/> with the message and arguments

    /// that are passed in. This allows a test to be cut short, with a result

    /// of success returned to NUnit.

    /// </summary>

    [DoesNotReturn]

    static public void Pass()

    {

        Assert.Pass(string.Empty, null);

    }