[英]MSTest fails Unit Test that passes in VS 2013
我一直在设置Cake脚本(C#Make)以准备自动化我们的单元测试。 我已经设置了脚本,除了一个怪癖之外,它正在按预期运行单元测试...
当MSTest尝试执行它时,较简单的测试之一失败了,但是当我从VS 2013运行测试时,它以飞色通过。
[TestMethod]
public void Field_is_XmlNode_innertext_urlencode()
{
// Arrange
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<field>\"'#</field>");
var fetchedField = xmlDoc.SelectSingleNode("field");
// Assert
Assert.AreEqual("%22%27%23", CrmHelpers.GetFetchedFieldValueAsString(fetchedField, null, true));
}
调用MSTest的Cake脚本部分。
Task("RunTests")
.IsDependentOn("Build")
.Does(() =>
{
Information("TestPath {0}", "./Tests/Unit Tests/**/bin/" + configuration + "/*.UnitTests.dll");
MSTest("./Tests/Unit Tests/**/bin/" + configuration + "/*.UnitTests.dll");
});
在VS2013中,值\\“'#已正确编码为%22%27%23,但是在通过Cake调用MSTest执行测试时,相同的值被编码为%22'%23。
我的问题是:为什么这些值在MSTest中的编码与我在VS2013中看到的在理论上应该以相同的代码运行时不同? 如何解决此问题,以便MSTest以可预测的一致方式运行我的所有单元测试?
编辑1:添加请求的方法
public static string GetFetchedFieldValueAsString(XmlNode fetchedField, string format, bool urlEncode)
{
var fieldValue = "";
if (format.IsNotEmpty())
{
try
{
if (fetchedField.Attributes["date"] != null)
{
var date = DateTime.Parse(fetchedField.InnerText).ToUniversalTime();
fieldValue = GetFormatedDateAsString(date, format);
}
else if (fetchedField.InnerText.IsNumeric())
{
var number = float.Parse(fetchedField.InnerText);
fieldValue = number.ToString(format);
}
}
catch
{
// Ignore formating errors
}
}
if (fieldValue.IsEmpty()) fieldValue = fetchedField.GetAttributeValue("name");
if (fieldValue.IsEmpty()) fieldValue = fetchedField.GetAttributeValue("formattedvalue");
if (fieldValue.IsEmpty() && fetchedField.Attributes["date"] != null)
{
var date = fetchedField.GetAttributeValue("date");
var time = fetchedField.GetAttributeValue("time");
if (date.IsNotEmpty()) fieldValue = date;
if (date.IsNotEmpty() && time.IsNotEmpty()) fieldValue += " ";
if (time.IsNotEmpty()) fieldValue += time;
}
if (fieldValue.IsEmpty()) fieldValue = fetchedField.InnerText;
if (fieldValue.IsGuid()) fieldValue = fieldValue.Replace("{", "").Replace("}", "");
fieldValue = fieldValue.Sanitize();
if (urlEncode) fieldValue = fieldValue.UrlComponentEncode();
return fieldValue;
}
编辑2:添加请求的方法
public static string UrlComponentEncode(this string s)
{
return Uri.EscapeDataString(s);
}
编辑3:蛋糕方法来获取MSTest路径
/// <summary>
/// Gets the default tool path.
/// </summary>
/// <returns>The default tool path.</returns>
protected override FilePath GetDefaultToolPath(MSTestSettings settings)
{
foreach (var version in new[] { "12.0", "11.0", "10.0" })
{
var path = GetToolPath(version);
if (_fileSystem.Exist(path))
{
return path;
}
}
return null;
}
private FilePath GetToolPath(string version)
{
var programFiles = _environment.GetSpecialPath(SpecialPath.ProgramFilesX86);
var root = programFiles.Combine(string.Concat("Microsoft Visual Studio ", version, "/Common7/IDE"));
return root.CombineWithFilePath("mstest.exe");
}
解决的办法就是不使用Cake。 回到PS解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.