1

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)

ConditionRacer
  • 4,418
  • 6
  • 45
  • 67
  • Note that it feels like unusual definition of unit test - you are testing 3 unrelated components - ScriptParser, probably your build system that packages scripts and scripts themselves. Usually you'd call test for ScriptParser.Parse a unit-test, but script of your scope integration test... – Alexei Levenkov Apr 20 '12 at 21:41
  • @AlexeiLevenkov: as far as I can see, OP is only testing `ScriptParser.Parse`. There's no other "components" involved except from loading test data from resources (wouldn't call it component though). Nothing wrong with this test (maybe except that `foreach`, but I'm not the one to judge). – k.m Apr 20 '12 at 21:46
  • I guess I'm misreading "part of the project" as "included in assembly/files of the product"... You are probably right as it should be read as "Zip archive of script files strictly for the tests". – Alexei Levenkov Apr 20 '12 at 21:52
  • @AlexeiLevenkov: that's what I assumed. I would argue tho, that keeping test resources in .zip is somehow confusing... why not simply have them as separate files and do some kind of data driven testing. Unless OP really wants to test zip-loading-extracting-parsing integration (but then again, it would be more of an integration test as you pointed). – k.m Apr 20 '12 at 21:55

2 Answers2

3

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

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
1

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?

Community
  • 1
  • 1
Mike Park
  • 10,845
  • 2
  • 34
  • 50