相当于mstest中的assert.warning?

发布于 2024-08-04 03:30:53 字数 47 浏览 1 评论 0原文

MbUnit 中是否有 Assert.Warning 的 MsTest 等效项?

is there a MsTest Equivalent of Assert.Warning in MbUnit ?

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

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

发布评论

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

评论(4

我们的影子 2024-08-11 03:30:53

最接近的匹配是 Assert.Inconclusive() - 它不会使测试失败,但也不会成功。它属于第三阶段,称为“不确定”。

单个不确定的测试将导致整个测试套件变得不确定。

还有一些重载也支持自定义消息:

Assert.Inconclusive("Ploeh");

The closest match is Assert.Inconclusive() - it doesn't make the test fail as such, but it doesn't succeed either. It fall into a third stage called Inconclusive.

A single Inconclusive test will cause an entire test suite to be Inconclusive.

There are overloads that supports custom messages as well:

Assert.Inconclusive("Ploeh");
风铃鹿 2024-08-11 03:30:53

当我在某些项目中使用 NUnit 时,我遇到了类似的问题。尝试使用

Console.Write("Some Warning");

I have a similar issue as I use NUnit for some projects. Try using

Console.Write("Some Warning");
岁吢 2024-08-11 03:30:53

您可能想要使用自定义异常。

Assert.Inconclusive 的问题是测试资源管理器指出测试甚至没有运行。在将来运行测试时,这可能会产生误导,特别是当测试由其他开发人员运行时:

在此处输入图像描述

我更喜欢的方式如下。首先,定义一个自定义的UnitTestWarningException。我给了我一个额外的构造函数,这样我就可以传递带有参数的 String.Format 样式的警告消息:

public class UnitTestWarningException : Exception
{
  public UnitTestWarningException(string Message) : base(Message) { }

  public UnitTestWarningException(string Format, params object[] Args) : base(string.Format(Format, Args)) { }
}

然后,在您想要以警告结束单元测试的地方,抛出一个 UnitTestWarningException相反:

[TestMethod]
public void TestMethod1()
{
  .
  .
  .
  try
  {
    WorkflowInvoker.Invoke(workflow1, inputDictionary);
  }
  catch (SqlException ex)
  {
    if (ex.Errors.Count > 0
      && ex.Errors[0].Procedure == "proc_AVAILABLEPLACEMENTNOTIFICATIONInsert")
    {
      //Likely to occur if we try to repeat an insert during development/debugging. 
      //Probably not interested--the mail has already been sent if we got as far as that proc.

      throw new UnitTestWarningException("Note: after sending the mail, proc_AVAILABLEPLACEMENTNOTIFICATIONInsert threw an exception. This may be expected depending on test conditions. The exception was: {0}", ex.Message);
    }
  }
}

结果:测试资源管理器然后显示测试已执行,但失败并出现 UnitTestWarningException,显示警告:

在此处输入图像描述

You may want to use a custom exception.

The trouble with Assert.Inconclusive is that Test Explorer states the test wasn't even run. This may be misleading when running the test in the future, particularly if the test is run by other developers:

enter image description here

The way I've come to prefer is as follows. Firstly, define a custom UnitTestWarningException. I've given mine an additional constructor so I can pass my warning message String.Format-style with arguments:

public class UnitTestWarningException : Exception
{
  public UnitTestWarningException(string Message) : base(Message) { }

  public UnitTestWarningException(string Format, params object[] Args) : base(string.Format(Format, Args)) { }
}

Then, at the point where you want to end a unit test with a warning, throw a UnitTestWarningException instead:

[TestMethod]
public void TestMethod1()
{
  .
  .
  .
  try
  {
    WorkflowInvoker.Invoke(workflow1, inputDictionary);
  }
  catch (SqlException ex)
  {
    if (ex.Errors.Count > 0
      && ex.Errors[0].Procedure == "proc_AVAILABLEPLACEMENTNOTIFICATIONInsert")
    {
      //Likely to occur if we try to repeat an insert during development/debugging. 
      //Probably not interested--the mail has already been sent if we got as far as that proc.

      throw new UnitTestWarningException("Note: after sending the mail, proc_AVAILABLEPLACEMENTNOTIFICATIONInsert threw an exception. This may be expected depending on test conditions. The exception was: {0}", ex.Message);
    }
  }
}

The result: Test Explorer then shows that the test has been executed, but failed with a UnitTestWarningException that shows your warning:

enter image description here

信仰 2024-08-11 03:30:53

这是我关于如何使用 nunit 发出警告的黑客(我知道这个问题是关于 mstest 的,但这也应该有效)。一如既往,我对任何改进都很感兴趣。这个方法对我有用。

背景:我有代码检查测试本身是否有正确的注释,并具有逻辑来检测是否有人在不更改注释的情况下复制并粘贴了另一个测试。这些是我希望向开发人员显示的警告,而正常的 Assert.Inconclusive 不会阻止实际测试的运行。有些专注于测试,清理重构阶段是删除警告。

任务:在运行所有其他断言后发出警告。这意味着甚至会在开发过程中的测试中通常出现的 Assert.Fail 之后显示警告。

实现:(最好为所有测试文件创建一个基类):

public class BaseTestClass
{
    public static StringBuilder Warnings;

    [SetUp]
    public virtual void Test_SetUp()
    {
            Warnings = new StringBuilder();
    }

    [TearDown]
    public virtual void Test_TearDown()
    {
        if (Warnings.Length > 0)
        {
            string warningMessage = Warnings.ToString();

            //-- cleared if there is more than one test running in the session
            Warnings = new StringBuilder(); 
            if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
            {
                Assert.Fail(warningMessage);
            }
            else
            {
                Assert.Inconclusive(warningMessage);
            }
        }
    }

测试用法

[Test]
public void Sample_Test()
{
    if (condition) Warning.AppendLine("Developer warning");
    Assert.Fail("This Test Failed!");
}

实际结果:

"This Test Failed!"  
"Developer warning" 

测试状态失败 -

红色测试通过并且出现警告,您将获得“不确定 - 黄色”状态。

Here is my hack on how to have warnings with nunit ( i know this question was about mstest, but this should work too). As always, I am interested in any improvements. This method is working for me.

Background: I have code which checks the tests themselves for correct comments and has logic to detect if someone has copied and pasted another test without changing comments. These are warnings I want to be shown to the developer without normal Assert.Inconclusive blocking the actual test from running. Some are focused on the test and the cleanup refactorings phase is to remove the warnings.

Mission: to have warnings after all other asserts are run. This means even showing the warnings after Assert.Fail that normally occur in tests during development.

Implementation: (best to create a base class for all test files):

public class BaseTestClass
{
    public static StringBuilder Warnings;

    [SetUp]
    public virtual void Test_SetUp()
    {
            Warnings = new StringBuilder();
    }

    [TearDown]
    public virtual void Test_TearDown()
    {
        if (Warnings.Length > 0)
        {
            string warningMessage = Warnings.ToString();

            //-- cleared if there is more than one test running in the session
            Warnings = new StringBuilder(); 
            if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
            {
                Assert.Fail(warningMessage);
            }
            else
            {
                Assert.Inconclusive(warningMessage);
            }
        }
    }

Testing Usage

[Test]
public void Sample_Test()
{
    if (condition) Warning.AppendLine("Developer warning");
    Assert.Fail("This Test Failed!");
}

Actual Result:

"This Test Failed!"  
"Developer warning" 

Status of test is failed - RED

If the test passed and there was a warning, you will then get the status of Inconclusive - YELLOW.

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