简体   繁体   中英

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. I want to test the class using NUnit with some pre generated test scripts.

I have the scripts in a zip file and included in the project. 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. It contains test scripts that should produce a known output that is testable. They're zipped because the scripts are rather large (100mb each)

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

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 :

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

C# natively doesn't have a way to manage zip files. You can use J# though.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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