Skip to content

Commit 88cd45a

Browse files
committed
remove debug section; add see also link
1 parent 30a7243 commit 88cd45a

File tree

1 file changed

+8
-62
lines changed

1 file changed

+8
-62
lines changed

docs/test/unit-testing-visual-csharp-code-in-a-store-app.md

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -209,68 +209,10 @@ You've set up the test and app projects and verified that you can run tests that
209209
> [!TIP]
210210
> Develop code by adding tests one at a time. Make sure that all the tests pass after each iteration.
211211
212-
## Debug a failing test
213-
214-
1. Add another new test named **NegativeRangeTest**:
215-
216-
```csharp
217-
// Verify that negative inputs throw an exception.
218-
[TestMethod]
219-
public void NegativeRangeTest()
220-
{
221-
string message;
222-
Rooter rooter = new Rooter();
223-
for (double v = -0.1; v > -3.0; v = v - 0.5)
224-
{
225-
try
226-
{
227-
// Should raise an exception:
228-
double actual = rooter.SquareRoot(v);
229-
230-
message = String.Format("No exception for input {0}", v);
231-
Assert.Fail(message);
232-
}
233-
catch (ArgumentOutOfRangeException ex)
234-
{
235-
continue; // Correct exception.
236-
}
237-
catch (Exception e)
238-
{
239-
message = String.Format("Incorrect exception for {0}", v);
240-
Assert.Fail(message);
241-
}
242-
}
243-
}
244-
```
245-
246-
2. Run the test and verify that it fails.
247-
248-
3. Choose the test name in **Test Explorer**. The failure message displays in the detail pane of Test Explorer.
249-
250-
![NegativeRangeTest failed in Test Explorer](../test/media/vs-2019/negativerangetest-fail.png)
251-
252-
The test looks for an <xref:System.ArgumentOutOfRangeException> and if it catches one, the test passes. In this case, the expected exception wasn't thrown.
253-
254-
4. Add code to the beginning of the **SquareRoot** method to check the input and throw an exception if it is negative:
255-
256-
```csharp
257-
public double SquareRoot(double x)
258-
{
259-
if (x < 0.0)
260-
{
261-
throw new ArgumentOutOfRangeException();
262-
}
263-
...
264-
```
265-
266-
5. Run all of the tests to make sure that you haven't introduced a regression.
267-
268-
All tests now pass.
269-
270-
![All tests pass in Test Explorer](../test/media/ute_ult_alltestspass.png)
271-
272212
## Refactor the code
273213

214+
In this section, you refactor both app and test code, then rerun tests to make sure they still pass.
215+
274216
### Simplify the square root estimation
275217

276218
1. Simplify the central calculation in the **SquareRoot** function by changing one line of code, as follows:
@@ -290,9 +232,9 @@ You've set up the test and app projects and verified that you can run tests that
290232

291233
### Eliminate duplicated code
292234

293-
The **RangeTest** method hard codes the denominator of the `tolerance` variable that's passed to the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> method. If you plan to add additional tests that use the same tolerance calculation, the use of a hard-coded value in multiple locations could lead to errors.
235+
The **RangeTest** method hard codes the denominator of the *tolerance* variable that's passed to the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.Assert> method. If you plan to add additional tests that use the same tolerance calculation, the use of a hard-coded value in multiple locations makes the code harder to maintain.
294236

295-
1. Add a private method to the **UnitTest1** class to calculate the tolerance value, and then call that method instead.
237+
1. Add a private helper method to the **UnitTest1** class to calculate the tolerance value, and then call that method from **RangeTest**.
296238

297239
```csharp
298240
private double ToleranceHelper(double expected)
@@ -319,3 +261,7 @@ The **RangeTest** method hard codes the denominator of the `tolerance` variable
319261

320262
> [!TIP]
321263
> If you add a helper method to a test class, and you don't want it to appear in **Test Explorer**, don't add the <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute> attribute to the method.
264+
265+
## See also
266+
267+
- [Walkthrough: Test-driven development using Test Explorer](quick-start-test-driven-development-with-test-explorer.md)

0 commit comments

Comments
 (0)