So, i'm new to unit testing, and even more so to test first development. Is it valid for me to have just a single assert.isTrue statement in my unit test where I pass in my method and a valid parameter, and compare it to the known good answer?
Method
public static string RemoveDash(string myNumber)
{
string cleanNumber = myNumber.Replace("-","");
return cleanNumber;
}
Test
[TestMethod()]
public void TestRemoveDash()
{
Assert.IsTrue(RemoveDash("50-00-0")=="50000");
}
Proper unit tests should fail for exactly one reason, that's why you should be using one assert per unit test.
A test without an assert (containing only an action with a possible setup) makes sure that the process works without an exception. In any other case, the test passes.
“One assertion per test” is a wise rule to keep in mind, because it helps you have tests that fail for a specific reason, and drives you to focus on a specific behavior at a time.
One Assertion in One Test Method To keep unit tests simple, it is best to include a single assertion in one test method. That means, one unit test should test one use-case and no more.
That's pretty valid if it tests the functionality of your method, which it very much seems to be doing.
Might consider using Equals here instead, but it doesn't really matter. Also, I know this is a test example, but always make sure to test cases where the input is not what is expected as well as whatever other valid forms it can come in (this can be in the same test method or a different one depending on your preference)
Testers sometimes read our tests so I attempt to make them as readble as possible. I would prefer to use the following, rather than the single Assert:
[TestMethod()]
public void TestRemoveDash()
{
string expected = "50000";
string actual = RemoveDash("50-00-0");
Assert.AreEqual(expected,actual);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With