UnitTest - Quando un test ha bisogno di un file di input

If I choose a side, He won't take me for a ride“

The Chamber Of 32 Doors - GENESIS - The Lamb Lies Down on Brodway - 1974

A volte un test ha bisogno di un file di input. Una soluzione e' certamente usare un mock, una soluzione meno bella (meno portabile) e' di scrivere un file sulla macchina di build, del developer, e su ogni altra macchina in cui serve da aprire nel test. La prima soluzione e' forse un po' sovradimensionata, la seconda e' complicata da manutenere. Oggi ho scoperto un metodo molto furbo che evita entrambi i problemi:

Si tratta di scrivere il contenuto del file come commento del test e di usare una semplicissima classe CommentReader per estrarre il contenuto in una stringa in modo da poterlo passare ad un assert. La comodita' e' che avendo davanti agli occhi il test ha anche ben in vista il “contenuto del file” che dobbiamo testare.

La fixture apparira' tipo questa:

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace BestBrains.System
{
 [TestFixture]
 public class CommentReaderTest
 {
  /// <sample>
  ///  <book>
  ///   <isbn>1234-1234</isbn>
  ///   <title>Code Complete</title>
  ///  </book>
  /// </sample>
  [Test]
  public void TestXmlComment()
  {
   string content = CommentReader.GetElement("sample");
   Assert.AreEqual(@"<book><isbn>1234-1234</isbn><title>Code Complete</title></book>", content);
  }

  /// <sample>
  ///  create table Orders ( id int, customer int );
  /// </sample>
  [Test]
  public void TestNonXmlComment()
  {
   string content = CommentReader.GetElement("sample");
   Assert.AreEqual(@"create table Orders ( id int, customer int );", content.Trim());
  }

 }
}

La classe da aggiungere ai propri progetti per leggere il commento/file e' la seguente.

using System;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Reflection;

namespace BestBrains.System
{
 public class CommentReader
 {
  private static XmlDocument generatedDocumentation = new XmlDocument();
  private static StackFrame callingMethodFrame;

  public static string GetElement(string elementName)
  {
   StackTrace stackTrace = new StackTrace();
   callingMethodFrame = stackTrace.GetFrame(1);

   generatedDocumentation.Load(String.Format("{0}.xml",
    Assembly.GetCallingAssembly().FullName.Split(',')[0]));

   XmlNode node = generatedDocumentation.SelectSingleNode(
    String.Format("doc/members/member[contains(@name, '{0}.{1}')]/{2}",
    callingMethodFrame.GetMethod().DeclaringType.ToString(),
    callingMethodFrame.GetMethod().Name,
    elementName));

   if (node != null)
    return node.InnerXml;
   else
    throw new ApplicationException(String.Format("Element '{0}' not found.", elementName));
  }
 }
}

Fonte: bestbrains.biz

Print | posted on lunedì 7 agosto 2006 20:47

Feedback

# Re: UnitTest - Quando un test ha bisogno di un file di input

left by papo at 08/08/2006 15:47 Gravatar
davvero molto interessante... grazie!
Comments have been closed on this topic.