일부 단위 테스트를 통해 많은 양의 데이터를 실행하려고합니다. 일종의 Excel 스프레드 시트 나 XML 문서에서이를 정의하고 싶습니다.단위 테스트 용 XML 데이터
이 데이터를 입력 및 예상으로로드하는 데 단위 테스트 프레임 워크를 사용할 수 있습니까?
예외 포착과 관련된 문제가있을 것으로 예상됩니다. 이것에 대한 의견도 감사합니다.
일부 단위 테스트를 통해 많은 양의 데이터를 실행하려고합니다. 일종의 Excel 스프레드 시트 나 XML 문서에서이를 정의하고 싶습니다.단위 테스트 용 XML 데이터
이 데이터를 입력 및 예상으로로드하는 데 단위 테스트 프레임 워크를 사용할 수 있습니까?
예외 포착과 관련된 문제가있을 것으로 예상됩니다. 이것에 대한 의견도 감사합니다.
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);
}
}
MBUnit은 XML 문서 또는 데이터베이스와 같은 데이터 소스에서 테스트를 수행 할 수 있습니다.
MSTest의 경우 DataSource 특성을 살펴보십시오.
때문에 데이터에는 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 Made glorious summer by this sun of York; And all the clouds that lour'd upon our house In the deep bosom of the ocean buried. "/>
<Test find="horse" expect="KING RICHARD III A horse! a horse! my kingdom for a horse! CATESBY "Withdraw, my lord; I'll help you to a horse.""/>
<Test find="blah blah" expect="(null)"/>
</TestStrings>