Does it really have unit tests with approval only?

So, I'm new to unit testing, and even more so for testing the first development. Is there really only one assert.isTrue statement for me in my unit test, where I pass in my method and a valid parameter and compare it with a well-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"); } 
+6
c # unit-testing tdd
source share
4 answers

This is very important if it checks the functionality of your method, which, apparently, is very important.

Maybe use Equals instead, but that doesn't really matter. In addition, I know that this is a test case, but always check for cases where the input is not expected, as well as any other valid forms that may occur (this may be the same test method or another as you wish)

+10
source share

Testers sometimes read our tests, so I try to make them as possible as possible. I would rather 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); } 
+10
source share

The only comment is to use Assert.AreEqual instead of Assert.IsTrue :

 Assert.IsAreEqual("50000", RemoveDash("50-00-0")); 

The reason for this is that if the test fails, the error message you receive describes in more detail what was supposed to happen and what actually happened. A message that says "Expected Value <50,000>, but actually <50-00-0>" is much better than "Expected Value", but it was false.

As a rule, whenever you want to use Assert.IsTrue , go through the Assert methods and see if there is a better way to check your expectation (for example, Assert.IsInstanceOfType , Assert.IsNotNull , etc.) ..

+6
source share

This seems completely true - however, why not include several other tests in this method, along the same lines, but testing, for example, RemoveDash("-") == "" and RemoveDash("-5") == "5" etc.

+2
source share

All Articles