C#单元测试,使用zip文件中的XML

时间:2012-04-20 21:33:23

标签: c# xml unit-testing

我有一个类,它接受一个字符串作为xml文件的路径并输出一个对象。我想使用NUnit和一些预先生成的测试脚本来测试该类。

我有一个zip文件中的脚本并包含在项目中。我想做这样的事情:

// Not sure how to do this
List<byte[]> scripts = GetTheScriptsSomehow();

foreach(var script in scripts )
{
  var parsedScript = ScriptParser.Parse(script);
  Assert.AreEqual(parsedScript.Blah, "BLAH");
}

我最关心的部分是如何访问压缩的脚本和项目的一部分。

谢谢!

编辑:为了解决一些注释,zip文件是单元测试项目的一部分,而不是已发布的代码库。它包含应该生成可测试的已知输出的测试脚本。它们是压缩的,因为脚本相当大(每个100mb)

2 个答案:

答案 0 :(得分:3)

在资源中添加此zip文件(项目属性/资源/添加资源/添加现有文件),并使用SharpZipLib从zip中提取它,假设您的zip文件是

  

scripts.zip

用字符串提取脚本的代码:

ZipInputStream zip = new ZipInputStream(new MemoryStream(Properties.Resources.scripts));
while (zip.GetNextEntry() != null)
{
  MemoryStream data = new MemoryStream();
  zip.CopyTo(data);
  data.Position = 0;
  string scriptContents = new StreamReader(data).ReadToEnd();
  /// do something with scriptContents variable
}

以下是SharpZipLib用法的一些示例:

http://wiki.sharpdevelop.net/SharpZipLib-Zip-Samples.ashx

答案 1 :(得分:1)

C#本身没有管理zip文件的方法。你可以使用J#。

http://msdn.microsoft.com/en-us/magazine/cc164129.aspx

或者您可以考虑通过命令行解压缩到临时目录中。

How to unzip a file using the command line?