1

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?

1 Answer 1

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I've manually added it. I am having to do it that way at the moment, but I'd much prefer to declare it once in the datasource and then have it accessible in the TestContext. Am I right in thinking that TestContext.DataRow.Table.TableName should be populated?
Yes, I also tried it and the TableName was always "Table1". Check my edited answer if this will help you. However, I still believe that it's more easy if you use a constant variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.