データ駆動型のNUnitでテスト?
-
26-09-2019 - |
質問
MSTestを、あなたはのような何かを行うことができます
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
"testdata.csv", "testdata#csv", DataAccessMethod.Sequential)]
public void TestSomething()
{
double column1 = Convert.ToDouble(TestContext.DataRow["column1"]);
...
Assert.AreEqual(...);
}
のNUnit 2.5での同等のコードは何ですか?
解決
私はまた、あなたがちょうどあなたを助けるために、非MSユニットテストフレームワークのライブラリを探しているなら、xUnit.netがこの機能を持っていることを指摘しなければなりません。ベン・ホールでからこのブログ記事をチェック
他のヒント
私は駆動csvファイルベースのデータは、作業のNUnitでテストしました。
使用テストでのIEnumerableを返すprivateメソッドに包まコードプロジェクトするから CSVリーダー、このクラスは、その後、あなたのテストケースにTestCaseSource属性でこれを参照します。プロジェクトとセットでCSVファイルを含める "のコピーを出力するディレクトリの" に "のコピーは必ず" の
using System.Collections.Generic;
using System.IO;
using LumenWorks.Framework.IO.Csv;
using NUnit.Framework;
namespace mytests
{
class MegaTests
{
[Test, TestCaseSource("GetTestData")]
public void MyExample_Test(int data1, int data2, int expectedOutput)
{
var methodOutput = MethodUnderTest(data2, data1);
Assert.AreEqual(expectedOutput, methodOutput, string.Format("Method failed for data1: {0}, data2: {1}", data1, data2));
}
private int MethodUnderTest(int data2, int data1)
{
return 42; //todo: real implementation
}
private IEnumerable<int[]> GetTestData()
{
using (var csv = new CsvReader(new StreamReader("test-data.csv"), true))
{
while (csv.ReadNextRecord())
{
int data1 = int.Parse(csv[0]);
int data2 = int.Parse(csv[1]);
int expectedOutput = int.Parse(csv[2]);
yield return new[] { data1, data2, expectedOutput };
}
}
}
}
}
のオリジナルのポスト:ます。http://timwise.blogspot .COM / 2011/05 /データ駆動型テスト・イン・NUnitの-WITH-csv.html の
はここでティム・アベルのしかしCSVリーダーのためのフレームワークを使用して、テストの詳細を示していないと非常に類似した別の例です。 (注)TestCaseAttributeを使用する場合TestAttributeを省略することができます。
[TestCaseSource("GetDataFromCSV")]
public void TestDataFromCSV(int num1,int num2,int num3)
{
Assert.AreEqual(num1 + num2 ,num3);
}
private IEnumerable<int[]> GetDataFromCSV()
{
CsvReader reader = new CsvReader(path);
while (reader.Next())
{
int column1 = int.Parse(reader[0]);
int column2 = int.Parse(reader[1]);
int column3 = int.Parse(reader[2]);
yield return new int[] { column1, column2, column3 };
}
}
public class CsvReader : IDisposable
{
private string path;
private string[] currentData;
private StreamReader reader;
public CsvReader(string path)
{
if (!File.Exists(path)) throw new InvalidOperationException("path does not exist");
this.path = path;
Initialize();
}
private void Initialize()
{
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
reader = new StreamReader(stream);
}
public bool Next()
{
string current = null;
if ((current = reader.ReadLine()) == null) return false;
currentData = current.Split(',');
return true;
}
public string this[int index]
{
get { return currentData[index]; }
}
public void Dispose()
{
reader.Close();
}
}
CSVデータ:
10200210 20190210 30180210 40170210 50160210 60150210 70140210 80130210 90120210 100110210
注:3列目は、最初の2つの列の和であり、これは、ユニットテストにアサートされる
結果:
TestCaseDataを使用する代替の下に検索オブジェクトと戻り型(必須でオフコース)
設定 [TestCaseSource("GetDataFromCSV2")]
public int TestDataFromCSV2(int num1, int num2)
{
return num1 + num2;
}
private IEnumerable GetDataFromCSV2()
{
CsvReader reader = new CsvReader(path);
while (reader.Next())
{
int column1 = int.Parse(reader[0]);
int column2 = int.Parse(reader[1]);
int column3 = int.Parse(reader[2]);
yield return new TestCaseData(column1, column2).Returns(column3);
}
}
MS試験は、異なるソースからのテストデータを持っているメカニズムを提供します。しかし、これは、NUnitのからボックスの利用できていないです。
私はいくつかのかなりの大きさであるテストのためのデータとコードを分離する使用に賛成しています。私は両方を分離すると、私の期待があります テストデータの1可読性 2.テストデータを修正するために簡単なはず ローカルおよびビルド環境で快適に実行する必要があります。3.ユニットテスト
JsonSectionReader の下には、私が持っているしたいというすべての機能を備えています。 このツールは、埋め込まれたJSONファイルからのデータのセクションを読み取ることができます。また、デシリアライズに非常に快適なメカニズムを提供しています。
https://www.nuget.org/packages/WonderTools.JsonSectionReader/
P.S。私はこのプロジェクトのメンテナだ、と私は私が望んでいた程度で、この問題を解決し、他のツールを見つけられませんでしたので、このプロジェクトが作成されました。
私はNUnitののequivilentセットアップ法などの方法をマークし、その後の試験に使用されるフィールドにデータをロードすることであると思います。
あなたは、多かれ少なかれ、それを自分でコーディングする必要があります。