2009-08-26 6 views
0

ユニットテストで大量のデータを実行しようとしています。これをExcelのスプレッドシートやXML文書の中に定義したいと考えています。ユニットテストのXMLデータ

このデータを入力および予測としてロードするためのユニットテストフレームワークを取得するには遠く離れていますか?

私はこれを予期することができますが、例外捕捉に関する問題があります。これに関するコメントも高く評価されています。

答えて

1

nUnitのフィクスチャはPOCOだけなので、実際には.NETクラスを使用するのが好きです。それは多くのデータをすることができますので、私はおそらく、スイート全体に対して一度だけ実行されTestFixtureSetUpでそれを設定します。(ただ、これを行う方法についての何かを探して数時間を過ごした

[TestFixture] 
public class Foo{ 

    private XmlDocument doc; 
    private BarClass bar; 

[TestFixtureSetUp] 
    public void FixtureSetUp(){ 
    doc = new XmlDocument(); 
    doc.Load("c:\file.xml"); 
    } 

    [SetUp] 
    public void SetUp(){ 
     BarClass = new BarClass(); 
    } 

    [Test] 
    public void TestX(){ 
    Assert.That(BarClass.DoSOmething(doc), Is.Baz); 
    } 

} 
0

MBUnitは、XMLドキュメントやデータベースなどのデータソースからテストを実行できます。

0

MSTestについては、DataSource属性を参照してください。

1

理由データにはフォーマットが組み込まれているため、CSVファイルを使用できませんでした)MSTestでXMLを使用する方法を工夫しました。

これは小さなサンプルです。それが役に立てば幸い。

はあなたが単純なクラスライブラリがあるとします。

public class GetStrings 
{ 
    public string RichardIII(string lookup) 
    { 
     string results; 
     switch(lookup) 
     { 
      case "winter": 
       { 
        results = 
         "Now is the winter of our discontent\nMade glorious summer by this sun of York;\nAnd all the clouds that lour'd upon our house\nIn the deep bosom of the ocean buried. "; 
        break; 
       } 
      case "horse": 
       { 
        results = 
         "KING RICHARD III \nA horse! a horse! my kingdom for a horse!\n\nCATESBY \n\"Withdraw, my lord; I'll help you to a horse.\""; 
        break; 
       } 
       default: 
       results = null; 
       break; 
     } 
     return results; 
    } 
} 

したがって、この方法の一部をチェックアウトしますユニットテストは、次のようになります。

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestStrings.xml", "Test", DataAccessMethod.Sequential), DeploymentItem("GetStringsTest\\TestStrings.xml"), TestMethod] 
public void RichardIIITest() 
{ 
    GetStrings target = new GetStrings(); 

    string lookup = TestContext.DataRow["find"].ToString(); 
    string expected = TestContext.DataRow["expect"].ToString(); 
    if (expected == "(null)") 
     expected = null; 

    string actual = target.RichardIII(lookup); 
    Assert.AreEqual(expected, actual); 
} 

xmlファイルのTestStrings.xmlのルックスをこのように:

<TestStrings> 
    <Test find="winter" expect="Now is the winter of our discontent&#10;Made glorious summer by this sun of York;&#10;And all the clouds that lour'd upon our house&#10;In the deep bosom of the ocean buried. "/> 
    <Test find="horse" expect="KING RICHARD III &#10;A horse! a horse! my kingdom for a horse!&#10;&#10;CATESBY &#10;&#34;Withdraw, my lord; I'll help you to a horse.&#34;"/> 
    <Test find="blah blah" expect="(null)"/> 
</TestStrings> 
関連する問題