MSTEST - 断言失败后继续

发布于 2025-01-05 08:34:43 字数 649 浏览 1 评论 0原文

我想知道是否有一种简单的方法可以在断言失败后完成测试。我们过去使用 Galileo 进行所有自动化测试,但我们已经迁移了 Visual Studio 测试框架。我们有一种方法可以让测试失败,但仍可以继续。

        public static bool DoAssertAndContinue(Action assert)
    {
        try
        {
            assert();
            return true;
        }
        catch (AssertionException ae)
        {
            ConfigContext.WriteLine(ae.Message);
            return false;
        }
    }

这是我们之前使用的......它会被这样调用:

assertionResults.Add(Automation.Utils.CommonMethods.DoAssertAndContinue(() => Assert.IsTrue(detail.ValidateName(boo, urns))));

我只是想找出模拟我们之前的最好方法,而不必重构我们所有的测试。

I am wonder if there is an easy way to finish a test after an Assert has failed. We used to use Galileo for all of our automated tested, but we have moved the Visual Studio Test framework. We had a method that would allow a test to fail, but continue on.

        public static bool DoAssertAndContinue(Action assert)
    {
        try
        {
            assert();
            return true;
        }
        catch (AssertionException ae)
        {
            ConfigContext.WriteLine(ae.Message);
            return false;
        }
    }

This is what we used before...and it would be called like this:

assertionResults.Add(Automation.Utils.CommonMethods.DoAssertAndContinue(() => Assert.IsTrue(detail.ValidateName(boo, urns))));

I am just trying to figure out the best way to emulate what we had before without having to refactor all of our tests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

開玄 2025-01-12 08:34:43

您现在应该捕获 UnitTestAssertException 这是所有 mstest 断言失败的基本异常。

Instead of AssertionException, you should now catch UnitTestAssertException which is the base exception for all mstest assert failures.

时光礼记 2025-01-12 08:34:43

您也可以在 MSTest 中使用 Try/Catch。在 Catch 块中,您可以捕获特定错误并使用 Console.Write 打印它以了解错误。我建议您查看这个 线程< /a> 了解更多详细信息。

编辑1:就我个人而言,我不使用try/catch来通过我的测试方法。我编写了一个测试方法来发现实际产品中的缺陷。因此,如果您期望您的调用方法会给您一些特定的异常,那么我建议使用 ExpectedException 属性。如果您正在针对单个测试数据运行测试方法,则这适用。

现在,如果您想将多个测试数据传递到您的测试方法中。然后我建议采用数据驱动的测试用例。在这里,您可以将所有测试数据保存在 XML 或 XLS 或数据库中。然后使用该输入文件,您可以将多个测试数据输入到您的测试方法中。如果您的调用方法向您发送一些异常,请尽量不要在此处使用 try/catch 以及任何测试数据,然后查看 MSTest 是否会处理它并移至下一个测试数据。如果它移动,那么在测试结果窗口中,您将能够看到为什么对于该特定测试数据,您的方法失败。有关数据驱动的概念,请参阅此链接

You can use Try/Catch in MSTest as well. In Catch block you can catch the specific error and print it using Console.Write to know about the error. I would recommend you look into this thread for more details.

EDIT 1: Personally I don't use try/catch to sake of passing my test method. I write a test method to find defect in actual product. So if you are expecting that your calling method will give you some specific exception then I would suggest to use ExpectedException attribute. This is applicable if you are running your test method for single test data.

Now if you want to pass multiple test data into your test method. Then I would suggest go for Data driven test cases. Here you can keep all your test data inside a XML or XLS or in a DB. Then using that input file you can feed multiple test data into your test method. Try not to use try/catch here and for any test data if your calling method send you some exception then see if MSTest will handle it and move to the next test data or not. If it moves then in Test result window you will be able to see why for that specific test data your method get failed. For data driven concept see this link

静赏你的温柔 2025-01-12 08:34:43

MsTest 框架会在断言失败时引发异常。它从单元测试方法中传播出去是导致测试失败的原因。如果您想继续失败的断言,您只需要处理该异常并防止它转义该方法。

The MsTest framework throws an exception on a failed assertion. It's propagation out of the unit test method is what causes the test to fail. If you want to continue on a failed assertion you just need to handle that exception and prevent it from escaping the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文