Is there any way to continue test after Assert? .. I need to see all cases which the assert causes.
foreach (var ex in data)
{
Assert.AreEqual(ex1, ex, msg);
}
Is there any way to continue test after Assert? .. I need to see all cases which the assert causes.
foreach (var ex in data)
{
Assert.AreEqual(ex1, ex, msg);
}
If you are using Visual Studio and the data you are talking about is just test data and not an actual collection of your program, you can feed the test data to your test method in the following way:
[TestMethod()]
[DataRow("1", "2")]
[DataRow("2", "3")]
[DataRow("3", "4")]
public void TheTest(string str1, string str2) {
Assert.AreEqual(str1, str2);
}
This way, each data case is treated as a separate test and is thus non-aborting. However, the output in Visual Studio may not be as compact as you like.
For this reason, I suggest building your own error message while looping the test data, where each fail is separated by a newline. If, at the end, you Assert.Fail() that full message, the Test Explorer window will list all the failed elements quite nicely, separated by a new line.
string errors = "";
[TestMethod()]
public void TheTest(...){
data.ForEach( if(wrong) errors += "\n it's wrong");
if(errors != "")
Assert.Fail(errors);
}
I recently was trying to do this for comparing to test files with file HASH strategy. This worked for me. FileAssert.AreEqual method return true or false in this case.
var equal = FileAssert.AreEqual(manifest.FileContents, storedFile);
if (!equal)
{
var fileText1 = Encoding.Default.GetString(manifest.FileContents);
var fileText2 = Encoding.Default.GetString(storedFile);
var dmp = DiffMatchPatchModule.Default;
var diffs = dmp.DiffMain(fileText1, fileText2, true);
var html = dmp.DiffPrettyHtml(diffs);
var filePath = Path.Combine(AssemblyDirectory, APP_DATA, "ManifestTestErrors.html");
File.WriteAllText(filePath, html);
Assert.False(!equal, $"Check {new Uri(filePath).AbsoluteUri} for more info");
}