How do you get the iteration / line number from Te

2020-06-09 04:49发布

I've implemented a data driven test using MsTest framework.

I was wondering if there was a way to get the iteration / line number of the current test code from the TestContext object?

As far as I can see, the only property relevant to DDT is DataRow that enables getting info for the current iteration from the data source, but I don't see any property that gives me what I need.

3条回答
我想做一个坏孩纸
2楼-- · 2020-06-09 04:52

this is without the DataSource attribute but i can tell you that it iterate by it self without the for loop

     for (int i = 0; i < x; i++)
        {
             int currentIteration = TestContext.DataRow.Table.Rows.IndexOf(TestContext.DataRow);
             DataTable dt = TestContext.DataRow.Table;
             int rowCount = dt.Rows.Count;

             DataRow secondRow = dt.Rows[i];
             string name = secondRow["name"].ToString();
             int Balance = Convert.ToInt32(secondRow["Balance"]);
             int Amount = Convert.ToInt32(secondRow["Amount"]);
             int Count = Convert.ToInt32(secondRow["Count"]);
             Assert.AreEqual(Balance, Amount);

        }
查看更多
虎瘦雄心在
3楼-- · 2020-06-09 04:54
private readonly PropertyInfo _rowIdProp = typeof(System.Data.DataRow).GetProperty("rowID", BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);

..

// No datarow means only one iteraton

var currentIteration = DataRow == null ? 1 : (Int64)_rowIdProp.GetValue(DataRow, null);
查看更多
Viruses.
4楼-- · 2020-06-09 05:14

Try this:

int currentIteration = TestContext.DataRow.Table.Rows.IndexOf(TestContext.DataRow);
查看更多
登录 后发表回答