4

単体テストに合格したかどうかを確認するために、2つのテキストの塊を比較して違いを確認する必要があります。残念ながら、テキストの長さは約500文字であり、1文字だけが異なる場合、問題がどこにあるかを検出することは非常に困難です。MSTestは、どの個々の文字が異なるかを教えてくれません。違いがあることを教えてくれます。

ユニットテスト時にこのようなテキストを比較するための最良の方法は何ですか?

(私はMSTestを使用しています(NUnitへの移行を検討しますが、すべてのテストがすでにMSTestで記述されているため、そうではありません)

4

3 に答える 3

6

そのようなシナリオのために特別に設計されたライブラリ承認テストがあります。の両方をサポートします。

ライブラリは、「ゴールデンコピー」テストアプローチを中心に構築されています。これは、マスターデータセットの信頼できるコピーであり、一度準備して検証します。

[TestClass]
public Tests
{
    [TestMethod]
    public void LongTextTest()
    {
        // act: get long-long text
        string longText = GetLongText();

        //assert: will compare longText variable with file 
        //    Tests.LongTextTest.approved.txt
        // if there is some differences, 
        // it will start default diff tool to show actual differences
        Approvals.Verify(longText);
    }
}
于 2013-02-01T10:57:12.587 に答える
2

MSTestCollectionAssertクラスを使用できます。

[TestMethod]
public void TestTest()
{
    string strA = "Hello World";
    string strB = "Hello World";

    // OK
    CollectionAssert.AreEqual(strA.ToCharArray(), strB.ToCharArray(), "Not equal!");

    //Uncomment that assert to see what error msg is when elements number differ
     //strA = "Hello Worl";
    //strB = "Hello World";
    //// Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: CollectionAssert.AreEqual failed. Not equal!(Different number of elements.)
    //CollectionAssert.AreEqual(strA.ToCharArray(), strB.ToCharArray(), "Not equal!");

    //Uncomment that assert to see what error msg is when elements are actually different
    //strA = "Hello World";
    //strB = "Hello Vorld";
    //// Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: CollectionAssert.AreEqual failed. Not equal!(Element at index 6 do not match.)
    //CollectionAssert.AreEqual(strA.ToCharArray(), strB.ToCharArray(), "Not equal!");

}
于 2013-02-01T10:44:52.487 に答える
1

比較を行うためのヘルパーを作成します。

if(!String.Equals(textA, textB, StringComparison.OrdinalIgnoreCase))
{
  int variesAtIndex = Utilities.DoByteComparison(textA,textB); // can be multiple, return -1 if all good
} // now assert on variesAtIndex`
于 2013-02-01T10:26:32.530 に答える