简体   繁体   English

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

[英]C# Unit testing, using XML from a zip file

I've got a class that takes a string for the path to an xml file and outputs an object. 我有一个类,它接受一个字符串作为xml文件的路径并输出一个对象。 I want to test the class using NUnit with some pre generated test scripts. 我想使用NUnit和一些预先生成的测试脚本来测试该类。

I have the scripts in a zip file and included in the project. 我有一个zip文件的脚本,并包含在项目中。 I want to do something like this: 我想做这样的事情:

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

The part I'm most concerned with is how to access the scripts that are zipped and part of the project. 我最关心的部分是如何访问压缩的脚本和项目的一部分。

Thanks! 谢谢!

Edit: To address some of the comments, the zip file is part of a unit test project, not the released codebase. 编辑:为了解决一些注释,zip文件是单元测试项目的一部分,而不是已发布的代码库。 It contains test scripts that should produce a known output that is testable. 它包含应该生成可测试的已知输出的测试脚本。 They're zipped because the scripts are rather large (100mb each) 它们是压缩的,因为脚本相当大(每个100mb)

Add this zip file in resources (project properties / resources / Add Resource / Add existing file), and use SharpZipLib to extract it from zip, let's say that your zip file is 在资源中添加此zip文件(项目属性/资源/添加资源/添加现有文件),并使用SharpZipLib从zip中提取它,假设您的zip文件是

scripts.zip scripts.zip

code to extract scripts in string : 用于提取字符串中脚本的代码:

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
}

Here are some examples of SharpZipLib usage : 以下是SharpZipLib用法的一些示例:

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

C# natively doesn't have a way to manage zip files. C#本身没有管理zip文件的方法。 You can use J# though. 你可以使用J#。

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

Or you might consider unzipping into a temporary directory via the command line. 或者您可以考虑通过命令行解压缩到临时目录。

How to unzip a file using the command line? 如何使用命令行解压缩文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM