简体   繁体   English

如何在 NUnit 测试/Selenium/C# 中将意外异常转换为断言失败?

[英]How to convert an unexpected exception into an assertion failure in NUnit test/Selenium/C#?

I am a newbie and couldn't make out in the NUnit documentation.我是新手,无法在 NUnit 文档中弄清楚。 I think this link contains useful information: https://docs.nunit.org/articles/nunit/writing-tests/constraints/ThrowsConstraint.html我认为此链接包含有用的信息: https://docs.nunit.org/articles/nunit/writing-tests/constraints/ThrowsConstraint.html

How to convert unexpected exception whether "element is not clickable", "click is intersepted" into an assertion failure.如何将“元素不可点击”、“点击被拦截”等意外异常转化为断言失败。

Without a convert, the [Retry] NUnit attribute does not restart failed tests on an unexpected exception.如果没有转换,[Retry] NUnit 属性不会在出现意外异常时重新启动失败的测试。

test suite structure测试套件结构

    [SetUp]
    public void Setup()
    {
        driver = new OpenQA.Selenium.Chrome.ChromeDriver();
        driver.Manage().Window.Maximize();
    }

    ....

    [Test]
    [Retry(3)]
    public void voidName()
    {
       Assert.That(() =>
            {
                 var name = new className(driver);
                 name.methodName();
            }, 
            Throws.Nothing);
       
    }
    .....

    [TearDown]
    public void TearDown()
    {
            driver.Quit();
    }

    

test method structure测试方法结构

public void methodName()
        {
            ....
            ....
          if(result == true)
           {
              Assert.Pass();
           }
          else
           {
              Assert.Fail();
           }
       


        }

Regardless of whether Pass or Fail has a methodName, voidName returns failed不管 Pass 还是 Fail 有 methodName,voidName 都返回 failed

Edit # 2:编辑#2:

In NUnit (or any unit testing framework) if a unit test passes any assertions, the program continues from that point normally.在 NUnit(或任何单元测试框架)中,如果单元测试通过了任何断言,程序将正常从该点继续。 If it fails an Assertion, the entire unit test fails at that point.如果断言失败,则整个单元测试在此时失败。

So it seems that you need to debug your code as it is not working as you expect.因此,您似乎需要调试代码,因为它没有按预期工作。 Your unit test is failing because it is encountering the Assert.Fail() in your logic.您的单元测试失败,因为它在您的逻辑中遇到Assert.Fail()

That being said, I don't think you should place your Assertions within the logic of your code.话虽如此,我认为您不应该将断言放在代码的逻辑中。 Instead, you should throw exceptions, and keep Assert statements only in your unit tests .相反,您应该抛出异常,并仅在单元测试中保留 Assert 语句 Otherwise, such issues will be difficult to avoid.否则,此类问题将难以避免。

public void methodName()
{
    ....
    ....
    if(result == true)
    { 
        // No need to call assert statements here
    }
    else
    {
        throw new System.Exception("Write your custom error message here.");
    }
    ....
}

Edit # 1:编辑#1:

Okay, now that you have shared your code, You can do as shown as Charlie mentioned好的,既然你已经分享了你的代码,你可以按照查理提到的那样做

[Test] 
[Retry(3)] 
public void DepositFoOpen() 
{
    Assert.That(
        () => 
        { 
            var MMFO = new MMFO_Tests_Lib.DepositFO(driver);
            MMFO.DepositFoOpen();
        }, Throws.Nothing); 
}

or if you want to use try-catch block as I mentioned:或者如果你想像我提到的那样使用 try-catch 块:

[Test] 
[Retry(3)] 
public void DepositFoOpen() 
{
    try
    {
        var MMFO = new MMFO_Tests_Lib.DepositFO(driver);
        MMFO.DepositFoOpen();
    }
    catch(Exception ex)
    {
        Assert.Fail();
    }
}

In this case, you want the ThrowsNothingConstraint在这种情况下,您需要ThrowsNothingConstraint

Assert.That(() => SomeMethod(), Throws.Nothing);

UPDATE: I'd give you more detail if your question had details.更新:如果您的问题有详细信息,我会给您更多详细信息。 :-) :-)

All I know from your question is that some method of yours causes an exception to be thrown.我从你的问题中所知道的是你的某些方法导致抛出异常。 I called that method "SomeMethod" 'cause that's all I know.我将该方法称为“SomeMethod”,因为我只知道这些。 I wrapped it in an Assert so that the error would be for the specific line of code, which causes the exception.我将它包装在一个 Assert 中,以便错误针对特定的代码行,这会导致异常。

If you don't know which method call causes the exception, then you should figure it out.如果您知道是哪个方法调用导致了异常,那么您应该弄清楚。 Most likely, it's some call you make to Selenium.很可能是您拨打了 Selenium。

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

相关问题 Nunit 异步测试异常断言 - Nunit async test exception assertion 如何使用带有NUnit的Selenium C#两次运行测试项目 - How to run a test project twice using Selenium C# with NUnit 测试执行未使用 NUnit 运行,Selenium 和 C# - Test execution not running with NUnit, Selenium and C# 使用Selenium和Nunit的并行测试失败 - Parallel test failure using Selenium and Nunit 如何在C#或nunit接口中调整nunit测试用例的速度? - How to adjust speed of nunit test cases in C# or in nunit interface? 在C#中使用Selenium WebDriver在每次测试后如何防止Nunit关闭浏览器 - How to prevent from Nunit closing browser after each test using Selenium WebDriver in C# 如何同时在多个浏览器上运行测试? 硒网格,C#,Specflow,NUnit - How do I run a test on multiple browsers at the same time? Selenium Grid, C#, Specflow, NUnit 如何在NUnit测试框架上使用Selenium C#在新的Chrome选项卡上打开URL - How to open a url on a new Chrome Tab using selenium C# over NUnit test framework 如何在用 C# 编写的 Selenium WebDriver(Nunit 测试用例)中按“Enter”? - How to press “Enter” in Selenium WebDriver (Nunit Test Case) written in C#? 如何正确测试? (C#,NUnit,Moq) - How to test this properly? (C#, NUnit, Moq)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM