Question

I have test methods decorated with a DataSource attribute like:

[DataSource(PROVIDER_INVARIANT_NAME, CONNECTION_STRING, 
"Test Case#", DataAccessMethod.Sequential), 
TestMethod]

with the test case number in MTM replacing "Test Case#". I'm trying to get that number within the unit test but TestContext.DataRow.Table.TableName is always "Table1". Can anyone tell me how to get the real value?

Was it helpful?

Solution

Unless I'm wrong, the "TestCase#" cannot be replaced by MTM, so propably you have manually added it in all your DataSource attributes.

This value is constant. Why don't you add a constant variable to your TestClass and then use it on both the DataSourceAttribute and your TestMethod?


EDIT
You can also access the DataSourceAttribute directly:

[TestClass]
public class TestClass
{
    public DataSourceAttribute DataSource
    {
        get
        {
            return (DataSourceAttribute)Attribute.GetCustomAttribute(typeof(TestClass).
                GetMethod("TestMethod"), typeof(DataSourceAttribute));
        }
    }

    [DataSource(PROVIDER_INVARIANT_NAME, CONNECTION_STRING, 
        "Test Case#", DataAccessMethod.Sequential), TestMethod]
    public void TestMethod()
    {
        string TestCaseId = DataSource.TableName;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top