我使用MsTests和Data Driven方法进行测试。 (Excel是测试的数据存储)
测试结果不提供有关测试数据的任何信息。 例如:
结果如下: testname(数据行5)。这对我来说并不清楚。
如何自定义输出测试结果? 例如 testname(测试数据(命名的单词,数字或行))
答案 0 :(得分:5)
我发现只有一个解决方案:
我有1000行的MyData.xlsx文件。简单逻辑:“A”列= 1,“B”列= A * 2 + 2。
第5,6,7行和第11,12,13行为零,表示失败。
我的测试的SourceCode:
public TestContext TestContext { get; set; }
[TestMethod]
[DataSource("System.Data.Odbc", "Dsn=Excel Files;dbq=|DataDirectory|\\TestData\\MyData.xlsx;", "TestDataSheet$", DataAccessMethod.Sequential)]
public void ExcelDataSource1000Items()
{
var errorMessage = "Passed";
int valueA = Convert.ToInt32(TestContext.DataRow["valueA"]);
int valueB = Convert.ToInt32(TestContext.DataRow["valueB"]);
try
{
Assert.AreEqual(valueA * 2 + 2, valueB);
}
catch (Exception exception)
{
errorMessage = exception.Message;
}
TestContext.WriteLine(string.Format("valueA: {0}, valueB: {1}, AssertResult: {2}", valueA, valueB, errorMessage));
}
使用参数:/ testcontainer,/ testsettings,/ resultsfile从MsTest命令行(用于生成* .trx文件)执行测试后,我们生成带有测试结果的* .trx文件。
如果我们打开这个* .trx文件,我们可以看到TextMessages:
<Results>
<UnitTestResult executionId="cfc325a7-647f-4ef3-bb08-777f7bb3ca37" testId="1dbff80d-1f74-5ea7-35fa-f2ecd1f9b9e4" testName="ExcelDataSource1000Items" computerName="AMDX6" duration="00:00:00.5764248" startTime="2012-09-06T03:21:45.2070571+04:00" endTime="2012-09-06T03:21:46.1251096+04:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="cfc325a7-647f-4ef3-bb08-777f7bb3ca37" resultType="DataDrivenTest">
<Output>
<TextMessages>
<Message>valueA: 1, valueB: 4, AssertResult: Passed</Message>
<Message>valueA: 2, valueB: 6, AssertResult: Passed</Message>
<Message>valueA: 3, valueB: 8, AssertResult: Passed</Message>
<Message>valueA: 4, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:<10>. Actual:<0>. </Message>
<Message>valueA: 5, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:<12>. Actual:<0>. </Message>
<Message>valueA: 6, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:<14>. Actual:<0>. </Message>
<Message>valueA: 7, valueB: 16, AssertResult: Passed</Message>
<Message>valueA: 8, valueB: 18, AssertResult: Passed</Message>
<Message>valueA: 9, valueB: 20, AssertResult: Passed</Message>
<Message>valueA: 10, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:<22>. Actual:<0>. </Message>
<Message>valueA: 11, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:<24>. Actual:<0>. </Message>
<Message>valueA: 12, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:<26>. Actual:<0>. </Message>
<Message>valueA: 13, valueB: 28, AssertResult: Passed</Message>
<Message>valueA: 14, valueB: 30, AssertResult: Passed</Message>
<Message>valueA: 15, valueB: 32, AssertResult: Passed</Message>
<Message>valueA: 16, valueB: 34, AssertResult: Passed</Message>
<Message>valueA: 17, valueB: 36, AssertResult: Passed</Message>
<Message>valueA: 18, valueB: 38, AssertResult: Passed</Message>
<Message>valueA: 19, valueB: 40, AssertResult: Passed</Message>
<Message>valueA: 20, valueB: 42, AssertResult: Passed</Message>
可以解析这些TextMessages以生成测试报告。 (例如)
P.S。对不起,我无法添加屏幕截图,因为我没有10个或更多的声誉= \