Equivalent of assert.warning in mstest?
Asked Answered
M

4

20

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

Maxon answered 2/9, 2009 at 19:50 Comment(1)
Good question. MSFT decided to discard existing unit testing standards and came up with something rather clunky instead.Watermelon
P
24

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");
Pomander answered 3/9, 2009 at 8:27 Comment(1)
Annoyingly, an Inconclusive test is enough to cause MSTEST.EXE to return the same status code (1) as a fail, so if you're using a batch file to set it all off you won't be able to tell the difference without more work.Lamelli
C
2

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

Console.Write("Some Warning");
Crum answered 5/4, 2010 at 13:54 Comment(1)
Assert.Inconclusive(); is supported by NUnit actually. nunit.org/index.php?p=utilityAsserts&r=2.5.8Chit
U
2

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

Unroof answered 2/9, 2016 at 7:46 Comment(0)
C
1

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.

Conveyancing answered 12/12, 2013 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.