Skip to content

Commit 7b3606b

Browse files
authored
Mention Assert.Throws and Assert.ThrowsExactly in docs (#44160)
1 parent 376d434 commit 7b3606b

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

docs/core/testing/mstest-analyzers/design-rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Design rules will help you create and maintain test suites that adhere to proper
1313
Identifier | Name | Description
1414
-----------|------|------------
1515
[MSTEST0004](mstest0004.md) | PublicTypeShouldBeTestClassAnalyzer | It's considered a good practice to have only test classes marked public in a test project.
16-
[MSTEST0006](mstest0006.md) | AvoidExpectedExceptionAttributeAnalyzer | Prefer `Assert.ThrowsException` or `Assert.ThrowsExceptionAsync` over `[ExpectedException]` as it ensures that only the expected call throws the expected exception. The assert APIs also provide more flexibility and allow you to assert extra properties of the exception.
16+
[MSTEST0006](mstest0006.md) | AvoidExpectedExceptionAttributeAnalyzer | Prefer `Assert.ThrowsExactly` or `Assert.ThrowsExactlyAsync` over `[ExpectedException]` as it ensures that only the expected call throws the expected exception. The assert APIs also provide more flexibility and allow you to assert extra properties of the exception.
1717
[MSTEST0015](mstest0015.md) | TestMethodShouldNotBeIgnored | Test methods should not be ignored (marked with `[Ignore]`).
1818
[MSTEST0016](mstest0016.md) | TestClassShouldHaveTestMethod | Test class should have at least one test method or be 'static' with method(s) marked by `[AssemblyInitialization]` and/or `[AssemblyCleanup]`.
1919
[MSTEST0019](mstest0019.md) | PreferTestInitializeOverConstructorAnalyzer | Prefer TestInitialize methods over constructors

docs/core/testing/mstest-analyzers/mstest0006.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ A method is marked with the `[ExpectedException]` attribute.
3333

3434
## Rule description
3535

36-
Prefer `Assert.ThrowsException` or `Assert.ThrowsExceptionAsync` over the `[ExpectedException]` attribute as it ensures that only the expected line of code throws the expected exception, instead of acting on the whole body of the test. The assert APIs also provide more flexibility and allow you to assert extra properties of the exception.
36+
Prefer `Assert.ThrowsException` or `Assert.ThrowsExceptionAsync` (or `Assert.ThrowsExactly`/`Assert.Throws` or `Assert.ThrowsExactlyAsync`/`Assert.ThrowsAsync` if using MSTest 3.8 and later) over the `[ExpectedException]` attribute as it ensures that only the expected line of code throws the expected exception, instead of acting on the whole body of the test. The assert APIs also provide more flexibility and allow you to assert extra properties of the exception.
3737

3838
```csharp
3939
[TestClass]
@@ -59,7 +59,7 @@ public class TestClass
5959

6060
## How to fix violations
6161

62-
Replace the usage of the `[ExpectedException]` attribute by a call to `Assert.ThrowsException` or `Assert.ThrowsExceptionAsync`.
62+
Replace the usage of the `[ExpectedException]` attribute by a call to `Assert.ThrowsException` or `Assert.ThrowsExceptionAsync` (or `Assert.ThrowsExactly`/`Assert.Throws` or `Assert.ThrowsExactlyAsync`/`Assert.ThrowsAsync` if using MSTest 3.8 and later).
6363

6464
```csharp
6565
[TestClass]
@@ -77,7 +77,7 @@ public class TestClass
7777
person.SetAge(-1);
7878

7979
// Act
80-
Assert.ThrowsException(() => person.GrowOlder());
80+
Assert.ThrowsExactly(() => person.GrowOlder());
8181
}
8282
}
8383
```

docs/core/testing/unit-testing-mstest-writing-tests-attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public class UnitTest1
390390
The MSTest framework introduced <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ExpectedExceptionAttribute> for marking a test method to expect an exception of a specific type. The test will pass if the expected exception is thrown and the exception message matches the expected message.
391391

392392
> [!WARNING]
393-
> This attribute exists for backward compatibility and is not recommended for new tests. Instead, use the `Assert.ThrowsException` method.
393+
> This attribute exists for backward compatibility and is not recommended for new tests. Instead, use the `Assert.ThrowsException` (or `Assert.ThrowsExactly` if using MSTest 3.8 and later) method.
394394
395395
## Metadata attributes
396396

docs/core/tutorials/testing-library-with-visual-studio-code.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The most common tests call members of the <xref:Microsoft.VisualStudio.TestTools
6767
| `Assert.IsFalse` | Verifies that a condition is `false`. The assert fails if the condition is `true`. |
6868
| `Assert.IsNotNull` | Verifies that an object isn't `null`. The assert fails if the object is `null`. |
6969

70-
You can also use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException%2A?displayProperty=nameWithType> method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown.
70+
You can also use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException%2A?displayProperty=nameWithType> (or `Assert.Throws` and `Assert.ThrowsExactly` if using MSTest 3.8 and later) method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown.
7171

7272
In testing the `StringLibrary.StartsWithUpper` method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return `true` in these cases, so you can call the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue%2A?displayProperty=nameWithType> method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return `false` in these cases, so you can call the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsFalse%2A?displayProperty=nameWithType> method.
7373

docs/core/tutorials/testing-library-with-visual-studio.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ The most common tests call members of the <xref:Microsoft.VisualStudio.TestTools
9595
| `Assert.IsFalse` | Verifies that a condition is `false`. The assert fails if the condition is `true`. |
9696
| `Assert.IsNotNull` | Verifies that an object isn't `null`. The assert fails if the object is `null`. |
9797

98-
You can also use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException%2A?displayProperty=nameWithType> method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown.
98+
You can also use the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.ThrowsException%2A?displayProperty=nameWithType> (or `Assert.Throws` and `Assert.ThrowsExactly` if using MSTest 3.8 and later) method in a test method to indicate the type of exception it's expected to throw. The test fails if the specified exception isn't thrown.
9999

100100
In testing the `StringLibrary.StartsWithUpper` method, you want to provide a number of strings that begin with an uppercase character. You expect the method to return `true` in these cases, so you can call the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue%2A?displayProperty=nameWithType> method. Similarly, you want to provide a number of strings that begin with something other than an uppercase character. You expect the method to return `false` in these cases, so you can call the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsFalse%2A?displayProperty=nameWithType> method.
101101

0 commit comments

Comments
 (0)