locked
Data Driven unit test RRS feed

  • Question

  • I am trying to run some test with different values each time.

    For example: I have3 methods: methodA methodB and methodC.

    I want to run each method twice with different Index location.

    I have a TestInitialize and ClassInitialize methods.

    1. I tried using ClassInitialize but i wasn't successful, I am getting compile time error because my test context  is not static. 

     

    publicTestContext TestContext {    get { return testContextInstance; }    set { testContextInstance = value; }  }

    [DeploymentItem("UI.Framework.IntegrationTests\\ExpectedValues.xml")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML""|DataDirectory|\\ExpectedValues.xml""searchTerm2"DataAccessMethod.Sequential)]

    [ClassInitialize()] publicstaticvoid ClassInitialize(TestContext context) {

      string indexPath = TestContext.DataRow[0].ToString();
      bool isSuccess1 = SearchApiTestsHelper.InitializeSearchEngine(indexPath, null);
      Assert.IsTrue(isSuccess1);

    }

    I couldn't do this.

    2. i tried using TestInitialize but the data row was not being loaded, it was null.

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML""|DataDirectory|\\ExpectedValues.xml""indices"DataAccessMethod.Sequential), TestInitialize()] publicvoid MyTestInitialize() {   string indexPath = TestContext.DataRow[0].ToString();   bool isSuccess1 = SearchApiTestsHelper.InitializeSearchEngine(indexPath, null);   Assert.IsTrue(isSuccess1);

    }

    3. I know i can set each method to run twice however, some methods require another data for valiadation.

    e.g method2 still requires to run twice for each index location and for each run assert a set of expected values

    from the data source.

     [DeploymentItem("UI.Framework.IntegrationTests\\ExpectedValues.xml")]         [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML""|DataDirectory|\\ExpectedValues.xml""pb1"DataAccessMethod.Sequential),         TestCategory("Integration")]         [TestMethod]         publicvoid PhraseBoosting()         {

    expectedVal =

                TestContext.DataRow[0].ToString();

            }

     How can this be archived?             


    Thursday, June 6, 2013 11:03 PM

Answers