Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests: Replace [ExpectedException] with Assert.Throws #34

Closed
stakx opened this issue Nov 13, 2016 · 0 comments
Closed

Unit tests: Replace [ExpectedException] with Assert.Throws #34

stakx opened this issue Nov 13, 2016 · 0 comments

Comments

@stakx
Copy link
Contributor

stakx commented Nov 13, 2016

There are several unit tests in the UnitTests project that check whether exceptions are thrown when they should be. This is done in two different ways:

  • By placing an [ExpectedException(typeof(TException))] custom attribute on the test method, or
  • by wrapping a method call or short code block with Assert.Throws<TException>(() => …);.

Using [ExpectedException] is problematic for at least two reasons:

  • It is too coarse-grained. It doesn't matter which part of a test method throws. However, speaking in terms of Arrange-Act-Assert, only the Act part of a test method should be tested for exceptions.
  • This attribute is no longer supported starting with NUnit version 3, so it would stand in your way if you wanted to migrate to a more recent version of NUnit.

Suggestion: For these two reasons (and, to a minor degree, for consistency's sake) all test methods using [ExpectedException] should be converted to Assert.Throws.

@stakx stakx changed the title Unit tests: Replace [ExpectedException] with Assert.Throws Unit tests: Replace [ExpectedException] with Assert.Throws Nov 13, 2016
@stakx stakx closed this as completed Nov 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant