C # Unit Testing: Why should the expected result be included in the test name?

If the expected result of the test method exists in the official documentation of the method, then specifying the expected result in the test names will be redundant. In fact, if the goal is to document the method, then the documentation should be written in the XML documentation of the method, which would provide Intellisense information and much more.

Why is everything but the test method really included in the test name?

+3
source share
7 answers

. TDD, . XML- , . ( .) , , (aka test) , . XML-.:) ( , . , , XML .)

, , / HTML. , . /, " 10- , 10 ". ( /), -.

+3

, , , 10+ , , , .

. 600 , , , , , .

+1

, - , /, . , , , pass/fail , , , :).. . , BDD , .

0

, , , . , / .

XML, . , , , ( ), .

0

. , , . , ..

MyMethod_Zero()
MyMethod_MinValue()
MyMethod_MaxValue()
MyMethod_SomeBranchTest()

.

0

PoppingEmptyStackThrowsInvalidOperationException() , StackPopTest_1()

0

, . . ( , ), , :

, :

public class ColorParser {
    public static bool TryParseHex(string value, out Color color) {
        // Code to parse the hex color value.
    }
}

- :

public class ColorParserTests
{
    public class TryParseHexTests
    {
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestNullValue()
        {
            Color color = Color.Empty;
            ColorParser.TryParseHex(null, out color);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestEmptyValue()
        {
            Color color = Color.Empty;
            ColorParser.TryParseHex(string.Empty, out color);
        }

        [TestMethod]
        public void TestInvalidValues()
        {
            string[] invalidValues = new string[] 
            { 
                "#",
                "FF",
                "#FF",
                "#FFAA",
                "asdf"
            };
            foreach (var invalidValue in invalidValues)
            {
                Color color = Color.Empty;
                bool result = ColorParser.TryParseHex(invalidValue, out color);
                Assert.IsFalse(result, string.Format("Value '{0}' must not pass the test!", invalidValue));
            }
        }

        [TestMethod]
        public void TestValidValues()
        {
            // Spaces are intended and a part of the test!
            Dictionary<string, Color> validValues = new Dictionary<string, Color>() 
            { 
                {"      #000000", Color.FromArgb(0,0,0)},
                {" #010203  ", Color.FromArgb(1,2,3)},
                {"#00FFFF", Color.FromArgb(0,255,255)},
                {"#FF00FFFF", Color.FromArgb(255,0,255,255)},
            };
            foreach (var validValue in validValues)
            {
                Color color = Color.Empty;
                bool result = ColorParser.TryParseHex(validValue.Key, out color);
                Assert.IsTrue(result, string.Format("Value '{0}' must pass the test!", validValue.Key));
                Assert.AreEqual(validValue.Value, color, "Parsed color must be the same.");
            }
        }
    }
}

, ( ), . , . , , , , exacly . .

, , , , , , - , .

Nulls Emptys , , , .

You can break the tests and write more tests, such as TestValuesWithSpacesor TestNegativeDepositor TestUserNameWithInvalidCharactersetc. It always depends how much you need to check and how exactly you want to do it. In this case, I found this sufficient. In any case, I think it is very descriptive.

0
source

Source: https://habr.com/ru/post/1776516/


All Articles