When writing unit tests, there are times when you can create an Assert for each condition that may fail, or an Assert that catches all such conditions. C # example:
Dictionary<string, string> dict = LoadDictionary(); // Optional Asserts: Assert.IsNotNull(dict, "LoadDictionary() returned null"); Assert.IsTrue(dict.Count > 0, "Dictionary is empty"); Assert.IsTrue(dict.ContainsKey("ExpectedKey"), "'ExpectedKey' not in dictionary"); // Condition actually interested in testing: Assert.IsTrue(dict["ExpectedKey"] == "ExpectedValue", "'ExpectedKey' is present but value is not 'ExpectedValue'");
Is it important for a large project with several people in a similar situation to add “Optional confirmations”? There is more work (if you have a lot of unit tests), but it will be more clear where the problem is.
I use VS 2010 and integrated testing tools, but I ask the question as a general one.
, - , , . , .
" ", . , , , , . ( TC) , , , - . , .
, - .
, , , "/ " aproach, , , (LoadDict(), ..), , " " , " ", LoadDictionary() , ( ). , , , . - / TC.
, , , , .
"" , .
LoadDictionary();
, . , , , .
, .
, , . , , / - .
, , , . , , , (, , ), .
, , Asserts, . , , Asserts , , - , .
, , ( ), , , , Asserts. , , dict null, ? , , , , , - .
dict
, , , - . , . , , , , , . - , , , .
. , , , .
, , .
, :
Assert.IsTrue(dict["ExpectedKey"] == "ExpectedValue", "'ExpectedKey' is present but value is not 'ExpectedValue'");
, LoadDictionary() null? unit test, C/++, . , dict null, .
: , ? , . , , (, ), : , , . .
Finally, using some kind of equality statement will give a more accurate error message:
Assert.Equal("ExpectedValue", dict["ExpectedKey"], "Incorrect value for 'ExpectedKey'");
The error should be something like "expected: ExpectedValue, actual:". Knowing the wrong value for a key can be helpful.
Source: https://habr.com/ru/post/1747909/More articles:Перенаправление заголовка PHP не перезагружаетв IE - internet-explorerHow to determine the size of a project (lines of code, function points, others) - project-managementJSF hides exceptions? - javaCounting the number of values between an interval - pythonUsing #define once for multiple source files - c ++Remove items and content before item - jqueryWhat is the correct way to use auto_ptr for dynamically allocated arrays? - c ++Архитектура REST/ROA. Отправлять поиск по базе данных/запрос/фильтр/параметры сортировки по URL-адресу? (>, <, IN и т.д.) - restPython - checking user admin privileges - pythonExclude records matching subquery - sqlAll Articles