English 中文(简体)
Unit Test for Exceptions Message
原标题:

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception.

[TestMethod()]
[ExpectedException(typeof(ArgumentException))]
public void ExceptionTestTest()
{
    throw new ArgumentException("BBB");
}

I would Like to have the test pass if the message in the exception is BBB , but fail if it is anything else. I looked at the second parameter of the ExpectedException attribute, but that is only a message to display in the test report if the Exception type is different.

I know I can try {} catch {} the exception an then assert that the message IsEqual to the message, but that feels clunky.

PS. I m using the built in Unit testing of Visual Studio 2008 (pro)

最佳回答

The built-in stuff in Visual Studio is poor. Take a look at NUnit. It s loads more sophisticated, has syntax helpers that make it easier to specific Asserts, can be run as part of your debugging or stand-alone, has a console runner as well as the UI and so on. I ve been using it for years - it s the business. The Exception message options give flavour of how sophisticated it is:

public enum MessageMatch
{
    /// Expect an exact match
    Exact,  
    /// Expect a message containing the parameter string
    Contains,
    /// Match the regular expression provided as a parameter
    Regex,
    /// Expect a message starting with the parameter string
    StartsWith
}

Don t be put off: it is sophisticated but it s not complicated - it s very easy to get working.

问题回答

No, that is unfortunately not possible with the current version of MSTest (much to my chagrin).

You may want to look at xUnit.NET s Assert.Throws for inspiration for better alternatives. Here are some pointers.





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签