MSTest无法通过VS 2013中的单元测试

时间:2015-02-04 16:13:34

标签: c# unit-testing encoding mstest

我已经设置了一个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中,值#&#34;#被正确编码为%22%27%23但是当通过Cake调用MSTest执行测试时,相同的值被编码为%22&#39;%23。

enter image description here enter image description here

我的问题是:为什么这些值在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路径的Cake方法

    /// <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");
    }

1 个答案:

答案 0 :(得分:0)

解决方案就是不要使用Cake。回到PS解决方案。

相关问题