2011年5月9日 星期一

[C#] 使用 Visual Studio Unit Testing Framework

The Visual Studio Unit Testing Framework describes Microsoft's suite of unit testing tools as integrated into Visual Studio 2005 and later.

Example: (Member Single Round Match 505 Round 1 - Division II, Level One)

Step 1: Add a test.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AlgorithmMatch;

namespace AlgorithmMatchTest {

[TestClass]
public class SentenceCapitalizerInatorTest {
public TestContext TestContext { get; set; }

[TestMethod]
public void RunSanityTest() {
var Q = new SentenceCapitalizerInator();

{
var P = "hello programmer. welcome to topcoder.";
var A = "Hello programmer. Welcome to topcoder.";
Assert.AreEqual(A, Q.fixCaps(P), false);
}
}
}

} // namespace AlgorithmMatchTest

Step 2: Run all tests and see if the new one fails
The new test fails.

Step 3: Write some code
using System;
using System.Text;

namespace AlgorithmMatch {

public class SentenceCapitalizerInator {
public String fixCaps(String paragraph) {
return paragraph;
}
}

} // namespace AlgorithmMatch

Step 4: Run all tests and see if the new one fails.
Fails. We should implement fixCaps() method correctly.

Step 5: Write some code
using System;
using System.Text;

namespace AlgorithmMatch {

public class SentenceCapitalizerInator {
public String fixCaps(String paragraph) {
var result = new StringBuilder();
bool is_began = true;
foreach (char c in paragraph) {
if (is_began && char.IsLetter(c)) {
result.Append(char.ToUpper(c));
is_began = false;
}
else if (c == '.') {
result.Append(c);
is_began = true;
} else {
result.Append(c);
}
}
return result.ToString();
}
}

} // namespace AlgorithmMatch

Step 6: Run the automated tests and see them succeed.
Passed.

Step 7: Repeat.
We may add more tests from system test dataset.

沒有留言:

張貼留言