6

I am writing some unit tests in which I need a fake xml file. I can create that file and require it to be deployed with the unit tests, but experience shows that at my office it's a lot of headache. So I decided that the file will be created by UT's.

StreamWriter sw = new StreamWriter(testFileName);
sw.Write(contents);
sw.Close();

Now the problem is the contents string. It is virtually a long xml, something like this:

string contents = 
@"<?xml version="1.0" encoding="windows-1251"?>
  <blah>
  ~100 lines here
  </blah> ";

I don't want this to be in the same file as the rest of the code. I want the string to be generated compile-time from a file.

In C++, I'd do this

string contents = "
#include "test.xml"
   ";

Is it possible somehow in C#?

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434

6 Answers6

9

Why don't you include it in a resource?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

You can do this with a T4 template. What you need is 3 things

  1. Create your template file, which will generate your .cs code
  2. Include your text file (or *.sql file) in the same project as your template file
  3. Configure your template to re-create your t4 template on every build

Something like this for your template.tt file

<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#
   string TextFromFile = File.ReadAllText(Host.ResolvePath("my_file_next_to_my_template.txt"));
#>
// This is the output code from your template
namespace MyNameSpace
{
    class MyGeneratedClass
    {
        static void main (string[] args)
        {
            System.Console.WriteLine("<#= TextFromFile #>");
        }
    }
}

If you don't want to configure your template to be re-created on the build then just opening it and saving it will do the trick.

Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
1

Use a resources (resx file) and add your xml file. Then you can access using MyNs.MyResources.MyXmlFile.

Jeff
  • 35,755
  • 15
  • 108
  • 220
0

I usually add such test files directly into the unit test C# project and set them as "Content" and "Copy if newer" to be copied to the bin directory during the compilation.

Igor Brejc
  • 18,714
  • 13
  • 76
  • 95
0

If the contents are static, why can't you commit it to source control? Add it to your project and then it will always be exactly where you expect it to be.

Matt Kellogg
  • 1,234
  • 8
  • 16
  • No, it won't. The unit test executables are copied to a different machine and run there – Armen Tsirunyan Jul 07 '11 at 15:51
  • 1
    I see. I made the assumption that whatever you were using for continuous integration would be doing the whole build process. In that case, the resource answer already provided is definitely the way to go. – Matt Kellogg Jul 08 '11 at 15:46
0

You can go the resource file route but, depending on how many resources/how large they are (and XML can be large), you may eventually run into compilation problems (I did).

I've since transitioned from resources to singletons. Reading a text file in with File.ReadAllText is pretty dead simple and it's still IntelliSense-friendly.

48klocs
  • 6,073
  • 3
  • 27
  • 34
  • I want to avoid the necessity of the existence of the text file at runtime. With resources, AFAIK, my executable can run standalone with no external file dependency. Is my understanding correct? – Armen Tsirunyan Jul 07 '11 at 16:07
  • @Armen: If you're running unit tests (which implies a development machine and access to source control), I don't know what the objection to having text files available is. Your understanding is correct though - resources can be embedded in DLLs/executables. – 48klocs Jul 07 '11 at 16:51
  • At my office a special team deals with builds and UT runs. They build the trunk, then copy all UT executables to a separate machine and run the UT's there. I can request them not to forget to take my file as well, but I don't want to introduce that dependency – Armen Tsirunyan Jul 07 '11 at 16:57