Skip to content

Commit

Permalink
Switch DataTestMethod to TestMethod (#43783)
Browse files Browse the repository at this point in the history
* Switch DataTestMethod to TestMethod

* Update sample code

* Update attributes doc as well
  • Loading branch information
Youssef1313 authored Nov 27, 2024
1 parent db88975 commit 1095be2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ Use the following elements to set up data-driven tests. For more information, se

- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute>
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute>
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute>
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute>
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute>

### `DataRowAttribute`

The `DataRowAttribute` allows you to run the same test method with multiple different inputs. It can appear one or multiple times on a test method. It should be combined with `TestMethodAttribute` or `DataTestMethodAttribute`.
The `DataRowAttribute` allows you to run the same test method with multiple different inputs. It can appear one or multiple times on a test method. It should be combined with `TestMethodAttribute`.

The number and types of arguments must exactly match the test method signature. Consider the following example of a valid test class demonstrating the `DataRow` attribute usage with inline arguments that align to test method parameters:

Expand Down
2 changes: 1 addition & 1 deletion docs/core/testing/unit-testing-visual-basic-with-mstest.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ In the *unit-testing-vb-mstest* directory, run `dotnet test` again. The `dotnet

## Adding more features

Now that you've made one test pass, it's time to write more. There are a few other simple cases for prime numbers: 0, -1. You could add those cases as new tests with the `<TestMethod>` attribute, but that quickly becomes tedious. There are other MSTest attributes that enable you to write a suite of similar tests. A `<DataTestMethod>` attribute represents a suite of tests that execute the same code but have different input arguments. You can use the `<DataRow>` attribute to specify values for those inputs.
Now that you've made one test pass, it's time to write more. There are a few other simple cases for prime numbers: 0, -1. You could add those cases as new tests with the `<TestMethod>` attribute, but that quickly becomes tedious. There are other MSTest attributes that enable you to write a suite of similar tests. You can use the `<DataRow>` attribute along with `<TestMethod>` attribute to specify values for those inputs.

Instead of creating new tests, apply these two attributes to create a single theory. The theory is a method that tests several values less than two, which is the lowest prime number:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Namespace PrimeService.Tests
Public Class PrimeService_IsPrimeShould
Private _primeService As Prime.Services.PrimeService = New Prime.Services.PrimeService()

<DataTestMethod>
<TestMethod>
<DataRow(-1)>
<DataRow(0)>
<DataRow(1)>
Expand All @@ -16,7 +16,7 @@ Namespace PrimeService.Tests
Assert.IsFalse(result, $"{value} should not be prime")
End Sub

<DataTestMethod>
<TestMethod>
<DataRow(2)>
<DataRow(3)>
<DataRow(5)>
Expand All @@ -27,7 +27,7 @@ Namespace PrimeService.Tests
Assert.IsTrue(result, $"{value} should be prime")
End Sub

<DataTestMethod>
<TestMethod>
<DataRow(4)>
<DataRow(6)>
<DataRow(8)>
Expand Down

0 comments on commit 1095be2

Please sign in to comment.