Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mstest name instead Data Row with Data-Driven testing

I use MsTests and Data Driven approach for testing. (Excel is data storage for tests)

tests result dont provide any information about tests data. For example:

Result look as: testname (Data row 5). And it is not clear for me.

How can i customise output test result? For example testname (Test data (word, number, or row named))

like image 430
alexey.chumagin Avatar asked Sep 05 '12 15:09

alexey.chumagin


1 Answers

I found only one solution:

I have MyData.xlsx file with 1000 rows. Simple logic: "A" column = 1, "B" column = A*2+2.

Rows 5,6,7 and 11,12,13 conteins zero for make a failed results.

SourceCode of my Test:

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));
    }

After execution test from MsTest command line (for generating *.trx file) with parameters: /testcontainer, /testsettings, /resultsfile we generate *.trx file with test results.

And if we open this *.trx file, we can see 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:&lt;10&gt;. Actual:&lt;0&gt;. </Message>
      <Message>valueA: 5, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:&lt;12&gt;. Actual:&lt;0&gt;. </Message>
      <Message>valueA: 6, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:&lt;14&gt;. Actual:&lt;0&gt;. </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:&lt;22&gt;. Actual:&lt;0&gt;. </Message>
      <Message>valueA: 11, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:&lt;24&gt;. Actual:&lt;0&gt;. </Message>
      <Message>valueA: 12, valueB: 0, AssertResult: Assert.AreEqual failed. Expected:&lt;26&gt;. Actual:&lt;0&gt;. </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>

These TextMessages can be parsed to generate test report. (for example)

P.S. Sorry, i can not add screenshots, because i dont have 10 or more reputation =\

like image 56
Vladimir Avdeev Avatar answered Nov 05 '22 15:11

Vladimir Avdeev